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
- #format_rest_errors(errors, context = nil) ⇒ Object
- #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.
13 14 15 |
# File 'lib/morpheus/cli/error_handler.rb', line 13 def initialize(io=$stderr) @stderr = io end |
Instance Method Details
#format_rest_errors(errors, context = nil) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/morpheus/cli/error_handler.rb', line 236 def format_rest_errors(errors, context=nil) out = "" Hash(errors).each do |key, value| if value.is_a?(Hash) out << format_rest_errors(value, context ? "#{context}.#{key}" : key) else prefix = context ? (context + '.') : '' out << "* #{prefix}#{key}: #{value}\n" end end out end |
#handle_error(err, options = {}) ⇒ Object
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 135 136 |
# File 'lib/morpheus/cli/error_handler.rb', line 17 def handle_error(err, ={}) exit_code = 1 = ( || {}).clone if Morpheus::Logging.debug? [:debug] = true end do_print_stacktrace = true #@stderr.puts "#{dark}Handling error #{err.class} - #{err}#{reset}" case (err) when SystemExit # nothing to do, assume the command that exited printed an error already # print "\n" cmdexit = err if cmdexit.success? exit_code, err = 0, nil else exit_code, err = cmdexit.status, "Command exited with status: #{cmdexit.status}" end return exit_code, err # when Morpheus::Cli::CommandNotFoundError # puts_angry_error err.message # @stderr.puts "Try help to see a list of available commands" # do_print_stacktrace = false # if err.exit_code # exit_code = err.exit_code # end when Morpheus::Cli::CommandAborted # user declined a confirmation prompt, do not print anything and exit 9 do_print_stacktrace = false exit_code = err.exit_code if err.exit_code # err = err.message return 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 err.optparse. if err.optparse && err.optparse. @stderr.puts "Try --help for more usage information" do_print_stacktrace = false # exit_code = 127 when Morpheus::Cli::CommandArgumentsError puts_angry_error err. if err.args.include?("--help") || err.args.include?("--help") @stderr.puts err.optparse else @stderr.puts err.optparse. if err.optparse && err.optparse. @stderr.puts "Try --help for more usage information" end do_print_stacktrace = false if err.exit_code exit_code = err.exit_code end when Morpheus::Cli::CommandError # this should probably always print the whole thing as red, but just does the first line for now. # until verify_args! replaces raise_command_error where the full parser help is in the error message.. = err..split(/\r?\n/) first_line = .shift puts_angry_error first_line if !.empty? @stderr.puts .join("\n") unless .empty? else if err.args.include?("--help") || err.args.include?("--help") @stderr.puts err.optparse else @stderr.puts err.optparse. if err.optparse && err.optparse. && .empty? @stderr.puts "Try --help for more usage information" end end do_print_stacktrace = false if err.exit_code exit_code = err.exit_code end when Morpheus::Cli::ExpressionParser::InvalidExpression # @stderr.puts "#{red}#{err.message}#{reset}" puts_angry_error err. do_print_stacktrace = false exit_code = 99 when SocketError @stderr.puts "#{red}Error Communicating with the remote appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when RestClient::Exceptions::Timeout @stderr.puts "#{red}Error Communicating with the remote appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when Errno::ECONNREFUSED @stderr.puts "#{red}Error Communicating with the remote appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when OpenSSL::SSL::SSLError @stderr.puts "#{red}Error Communicating with the remote appliance.#{reset}" @stderr.puts "#{red}#{err.}#{reset}" when RestClient::Exception print_rest_exception(err, ) # no stacktrace for now... return exit_code, err 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 -V or --debug for more verbose debugging information." end end return exit_code, err end |
#print_rest_errors(response, options = {}) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/morpheus/cli/error_handler.rb', line 206 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 # puts as_yaml(response['errors']) @stderr.print format_rest_errors(response['errors']) 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/morpheus/cli/error_handler.rb', line 151 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 begin print_rest_errors(JSON.parse(err.response.to_s), ) rescue TypeError, JSON::ParserError => ex end elsif err.response.code == 404 begin print_rest_errors(JSON.parse(err.response.to_s), ) rescue TypeError, JSON::ParserError => ex # not json, just 404 @stderr.print red, "Error Communicating with the remote appliance. #{e}", reset, "\n" end else @stderr.print red, "Error Communicating with the remote 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 -V or --debug for more verbose debugging information." end end else @stderr.print red, "Error Communicating with the remote appliance. #{e}", reset, "\n" end # uh, having this print method return exit_code, err to standardize return values of methods that are still calling it, at the end just by chance.. # return exit_code, err return 1, err end |
#print_rest_exception_request_and_response(e) ⇒ Object
272 273 274 275 276 277 278 279 280 281 |
# File 'lib/morpheus/cli/error_handler.rb', line 272 def print_rest_exception_request_and_response(e) @stderr.puts "#{red}Error Communicating with the remote appliance. (HTTP #{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
249 250 251 252 253 254 |
# File 'lib/morpheus/cli/error_handler.rb', line 249 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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/morpheus/cli/error_handler.rb', line 256 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
138 139 140 141 |
# File 'lib/morpheus/cli/error_handler.rb', line 138 def print_stacktrace(err) @stderr.print red, "\n", "#{err.class}: #{err.}", "\n", reset @stderr.print err.backtrace.join("\n"), "\n\n" end |