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', 'Consolle::Errors::PromptDetectionError' => 'PROMPT_DETECTION_ERROR' }.freeze
Class Method Summary collapse
Class Method Details
.classify_message(error_message) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/consolle/errors.rb', line 171 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/consolle/errors.rb', line 151 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 |