Exception: Alula::AlulaError
- Defined in:
- lib/alula/errors.rb
Direct Known Subclasses
ApiGatewayError, BadRequestError, ForbiddenError, InsufficientScopeError, InvalidFilterFieldError, InvalidRelationshipError, InvalidRequestError, InvalidRoleError, InvalidSortFieldError, InvalidTokenError, M2MError, NotConfiguredError, NotFoundError, ProcedureError, RateLimitError, ServerError, UnknownApiError, UnknownError, ValidationError
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
Class Method Summary collapse
-
.critical_error_for_response(error_text) ⇒ Object
Handle HTML-based errors from the API.
-
.error_for_response(response) ⇒ Object
Figure out what error should be raised.
-
.errors_for_response(response) ⇒ Object
Some responses have an error array.
- .for_response(response) ⇒ Object
-
.gateway_error_for_response(error_text) ⇒ Object
Handle Gateway errors from the API.
Instance Method Summary collapse
-
#initialize(error) ⇒ AlulaError
constructor
A new instance of AlulaError.
- #ok? ⇒ Boolean
Constructor Details
#initialize(error) ⇒ AlulaError
Returns a new instance of AlulaError.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/alula/errors.rb', line 7 def initialize(error) if error.class == String = error return end @http_status = error.http_status @raw_response = error @error = error.data['error'] = error.data['error_description'] || error.data['message'] end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
5 6 7 |
# File 'lib/alula/errors.rb', line 5 def error @error end |
#http_status ⇒ Object (readonly)
Returns the value of attribute http_status.
5 6 7 |
# File 'lib/alula/errors.rb', line 5 def http_status @http_status end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
5 6 7 |
# File 'lib/alula/errors.rb', line 5 def end |
#raw_response ⇒ Object (readonly)
Returns the value of attribute raw_response.
5 6 7 |
# File 'lib/alula/errors.rb', line 5 def raw_response @raw_response end |
Class Method Details
.critical_error_for_response(error_text) ⇒ Object
Handle HTML-based errors from the API
52 53 54 |
# File 'lib/alula/errors.rb', line 52 def self.critical_error_for_response(error_text) InvalidRequestError.new(error_text.first.first) end |
.error_for_response(response) ⇒ Object
Figure out what error should be raised
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 |
# File 'lib/alula/errors.rb', line 62 def self.error_for_response(response) case response.data['error'] when 'RateLimit' Alula.logger.error response RateLimitError.new(response) when 'invalid_token' InvalidTokenError.new(response) when 'insufficient_scope' InsufficientScopeError.new(response) when 'server_error' Alula.logger.error response ServerError.new(response) when 'Bad Request' BadRequestError.new(response) when 'Not Found' NotFoundError.new(response) else # # RPC errors are identified by jsonrpc in the body. return ProcedureError.new(response) if response.data['jsonrpc'] # Error messages coming from the API are inconsistent. # Sometimes they are in the msg key, sometimes in the message key # Example: https://github.com/alula-net/alula-connect/pull/3417#issue-1985813211 # Here we have some errors that are actually known but at least one of them is not properly formatted by the API = response.data.dig('error', 'data', 'msg') || response.data.dig('error', 'data', 'message') || response.data.dig('error', 'description') || response.data['message'] case when 'dealer does not exist' NotFoundError.new() when 'Insufficient User Scope' InsufficientScopeError.new() when 'Not Found' NotFoundError.new() when 'Device Not Found' NotFoundError.new() when 'Unable to unregister device' NotFoundError.new() else Alula.logger.error response raise NotImplementedError, "Unable to derive error for #{response.data['error'].to_json}" end end end |
.errors_for_response(response) ⇒ Object
Some responses have an error array. We won’t be able to raise on all errors but we can raise on the first one
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/alula/errors.rb', line 110 def self.errors_for_response(response) error = response.data['errors'].first case error['title'] when 'RateLimit' RateLimitError.new(response) when 'Forbidden' ForbiddenError.new(error['detail']) when 'Not Found' NotFoundError.new(error['detail']) when 'Bad Request' Alula.logger.error response BadRequestError.new(error['detail']) when 'API Error Unknown' Alula.logger.error response UnknownError.new(error['detail']) when 'Insufficient Scope' InsufficientScopeError.new(error['detail']) else # # RPC errors are identified by jsonrpc in the body. return ProcedureError.new(response) if response.data['jsonrpc'] # See comment in the error_for_response method = error.dig('data', 'msg') || error.dig('data', 'message') || error['description'] || error.dig('detail', 'description') case when 'dealer does not exist' NotFoundError.new() when 'Insufficient User Scope' InsufficientScopeError.new() when 'Not Found' NotFoundError.new() when 'Device Not Found' NotFoundError.new() when 'Unable to unregister device' NotFoundError.new() when 'Customer has cameras - cameras must be removed first' BadRequestError.new() else Alula.logger.error response raise NotImplementedError, "Unable to derive error for #{response.data['errors'].to_json}" end end end |
.for_response(response) ⇒ Object
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 |
# File 'lib/alula/errors.rb', line 23 def self.for_response(response) if !response.data['error'].nil? && !response.data['error'].empty? self.error_for_response(response) elsif !response.data['errors'].nil? && !response.data['errors'].empty? self.errors_for_response(response) elsif !response.data['message'].nil? self.error_for_response(response) elsif response.data.match(/^<!DOCTYPE html>/) self.critical_error_for_response(response.data.scan(/<pre>(.*)<\/pre>/)) elsif response.data.match(/502 Bad Gateway/) self.gateway_error_for_response(response.data.scan(/502 Bad Gateway/)) else = "Unable to derive error from response: #{response.inspect}" Alula.logger.error raise UnknownApiError.new() end rescue NoMethodError = "Unable to derive error from response: #{response.inspect}" Alula.logger.error raise UnknownApiError.new() end |
.gateway_error_for_response(error_text) ⇒ Object
Handle Gateway errors from the API
57 58 59 |
# File 'lib/alula/errors.rb', line 57 def self.gateway_error_for_response(error_text) ApiGatewayError.new(error_text.first) end |
Instance Method Details
#ok? ⇒ Boolean
19 20 21 |
# File 'lib/alula/errors.rb', line 19 def ok? false end |