Class: Morpheus::Cli::ErrorHandler
- Inherits:
-
Object
- Object
- Morpheus::Cli::ErrorHandler
- Includes:
- Term::ANSIColor
- Defined in:
- lib/morpheus/cli/error_handler.rb
Instance Method Summary collapse
- #handle_error(err, options = {}) ⇒ Object
-
#initialize(io = $stderr) ⇒ ErrorHandler
constructor
A new instance of ErrorHandler.
- #print_rest_errors(response, options = {}) ⇒ Object
- #print_rest_exception(err, options) ⇒ Object
- #print_rest_exception_request_and_response(e) ⇒ Object
- #print_rest_request(req) ⇒ Object
- #print_rest_response(res) ⇒ Object
- #print_stacktrace(err) ⇒ Object
Constructor Details
#initialize(io = $stderr) ⇒ ErrorHandler
Returns a new instance of ErrorHandler.
12 13 14 |
# File 'lib/morpheus/cli/error_handler.rb', line 12 def initialize(io=$stderr) @stderr = io end |
Instance Method Details
#handle_error(err, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/morpheus/cli/error_handler.rb', line 16 def handle_error(err, ={}) exit_code = 1 # heh if Morpheus::Logging.debug? && [:debug].nil? [:debug] = true end do_print_stacktrace = true case (err) when ::OptionParser::InvalidOption, ::OptionParser::AmbiguousOption, ::OptionParser::MissingArgument, ::OptionParser::InvalidArgument, ::OptionParser::NeedlessArgument # raise err # @stderr.puts "#{red}#{err.message}#{reset}" puts_angry_error err. @stderr.puts "Try -h for help with this command." do_print_stacktrace = false # exit_code = 127 when Morpheus::Cli::CommandError # @stderr.puts "#{red}#{err.message}#{reset}" puts_angry_error err. do_print_stacktrace = false # @stderr.puts "Try -h for help with this command." when SocketError @stderr.puts "#{red}Error Communicating with the Appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when RestClient::Exceptions::Timeout @stderr.puts "#{red}Error Communicating with the Appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when Errno::ECONNREFUSED @stderr.puts "#{red}Error Communicating with the Appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" # @stderr.puts "Try -h for help with this command." when OpenSSL::SSL::SSLError @stderr.puts "#{red}Error Communicating with the Appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when RestClient::Exception print_rest_exception(err, ) # no stacktrace for now... return exit_code # if !options[:debug] # return exit_code # end when ArgumentError @stderr.puts "#{red}Argument Error: #{err.}#{reset}" else @stderr.puts "#{red}Unexpected Error#{reset}" end if do_print_stacktrace if [:debug] if err.is_a?(Exception) print_stacktrace(err) else @stderr.puts err.to_s end else @stderr.puts "Use --debug for more information." end end return exit_code end |
#print_rest_errors(response, options = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/morpheus/cli/error_handler.rb', line 136 def print_rest_errors(response, ={}) begin if [:json] @stderr.print red @stderr.print JSON.pretty_generate(response) @stderr.print reset, "\n" else if !response['success'] @stderr.print red,bold if response['msg'] @stderr.puts response['msg'] end if response['errors'] response['errors'].each do |key, value| @stderr.print "* #{key}: #{value}\n" end end @stderr.print reset else # this should not really happen @stderr.print cyan,bold, "\nSuccess!" end end ensure @stderr.print reset end end |
#print_rest_exception(err, options) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/morpheus/cli/error_handler.rb', line 93 def print_rest_exception(err, ) e = err # heh if Morpheus::Logging.debug? && [:debug].nil? [:debug] = true end if err.response if [:debug] begin print_rest_exception_request_and_response(e) ensure @stderr.print reset end return end if err.response.code == 400 response = JSON.parse(err.response.to_s) print_rest_errors(response, ) else @stderr.print red, "Error Communicating with the Appliance. #{e}", reset, "\n" if [:json] || [:debug] begin response = JSON.parse(e.response.to_s) # @stderr.print red @stderr.print JSON.pretty_generate(response) @stderr.print reset, "\n" rescue TypeError, JSON::ParserError => ex @stderr.print red, "Failed to parse JSON response: #{ex}", reset, "\n" # @stderr.print red @stderr.print response.to_s @stderr.print reset, "\n" ensure @stderr.print reset end else @stderr.puts "Use --debug for more information." end end else @stderr.print red, "Error Communicating with the Appliance. #{e}", reset, "\n" end end |
#print_rest_exception_request_and_response(e) ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/morpheus/cli/error_handler.rb', line 187 def print_rest_exception_request_and_response(e) @stderr.puts "#{red}Error Communicating with the Appliance. (#{e.response.code})#{reset}" response = e.response request = response.instance_variable_get("@request") @stderr.print red print_rest_request(request) @stderr.print "\n" print_rest_response(response) @stderr.print reset end |
#print_rest_request(req) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/morpheus/cli/error_handler.rb', line 164 def print_rest_request(req) @stderr.print "Request:" @stderr.print "\n" @stderr.print "#{req.method.to_s.upcase} #{req.url.inspect}" @stderr.print "\n" end |
#print_rest_response(res) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/morpheus/cli/error_handler.rb', line 171 def print_rest_response(res) # size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size) size = (res.body.nil? ? 0 : res.body.size) @stderr.print "Response:" @stderr.print "\n" display_size = Filesize.from("#{size} B").pretty rescue size @stderr.print "HTTP #{res.net_http_res.code} - #{res.net_http_res.} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{display_size}" @stderr.print "\n" begin @stderr.print JSON.pretty_generate(JSON.parse(res.body)) rescue @stderr.print res.body.to_s end @stderr.print "\n" end |
#print_stacktrace(err) ⇒ Object
80 81 82 83 |
# File 'lib/morpheus/cli/error_handler.rb', line 80 def print_stacktrace(err) @stderr.print red, "\n", "#{err.class}: #{err.}", "\n", reset @stderr.print err.backtrace.join("\n"), "\n\n" end |