Class: Consolle::Errors::ErrorClassifier
- Inherits:
-
Object
- Object
- Consolle::Errors::ErrorClassifier
- Defined in:
- lib/consolle/errors.rb
Overview
Error classifier to map exceptions to error codes
Constant Summary collapse
- ERROR_CODE_MAP =
{ 'Timeout::Error' => 'EXECUTION_TIMEOUT', 'Consolle::Errors::SocketTimeout' => 'SOCKET_TIMEOUT', 'Consolle::Errors::RequestTimeout' => 'REQUEST_TIMEOUT', 'Consolle::Errors::ExecutionTimeout' => 'EXECUTION_TIMEOUT', 'SyntaxError' => 'SYNTAX_ERROR', '::SyntaxError' => 'SYNTAX_ERROR', 'LoadError' => 'LOAD_ERROR', '::LoadError' => 'LOAD_ERROR', 'NameError' => 'NAME_ERROR', 'NoMethodError' => 'NO_METHOD_ERROR', 'ArgumentError' => 'ARGUMENT_ERROR', 'TypeError' => 'TYPE_ERROR', 'ZeroDivisionError' => 'ZERO_DIVISION_ERROR', 'RuntimeError' => 'RUNTIME_ERROR', '::RuntimeError' => 'RUNTIME_ERROR', 'StandardError' => 'STANDARD_ERROR', 'Exception' => 'EXCEPTION', 'Consolle::Errors::ServerUnhealthy' => 'SERVER_UNHEALTHY' }.freeze
Class Method Summary collapse
Class Method Details
.classify_message(error_message) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/consolle/errors.rb', line 106 def self.() case when /syntax error/i 'SYNTAX_ERROR' when /cannot load such file|no such file to load/i 'LOAD_ERROR' when /undefined local variable or method/i, /undefined method/i 'NAME_ERROR' when /wrong number of arguments/i 'ARGUMENT_ERROR' when /execution timed out/i 'EXECUTION_TIMEOUT' when /request timed out/i 'REQUEST_TIMEOUT' else 'EXECUTION_ERROR' end end |
.to_code(exception) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/consolle/errors.rb', line 86 def self.to_code(exception) return 'UNKNOWN_ERROR' unless exception.is_a?(Exception) # Try exact class match first error_code = ERROR_CODE_MAP[exception.class.name] return error_code if error_code # Try with leading :: for core Ruby errors error_code = ERROR_CODE_MAP["::#{exception.class.name}"] return error_code if error_code # Check inheritance chain exception.class.ancestors.each do |klass| error_code = ERROR_CODE_MAP[klass.name] return error_code if error_code end 'UNKNOWN_ERROR' end |