Class: EasyMeli::ErrorParser
- Inherits:
-
Object
- Object
- EasyMeli::ErrorParser
- Defined in:
- lib/easy_meli/error_parser.rb
Constant Summary collapse
- ERROR_FIELDS =
%w[message error error_description]
- ERROR_LIST =
{ 'Error validating grant' => EasyMeli::InvalidGrantError, 'The User ID must match the consultant\'s' => EasyMeli::ForbiddenError, 'invalid_token' => EasyMeli::InvalidTokenError, 'Malformed access_token' => EasyMeli::MalformedTokenError, 'too_many_requests' => EasyMeli::TooManyRequestsError, 'unknown_error' => EasyMeli::UnknownError }
- STATUS_ERRORS =
{ 401 => EasyMeli::InvalidTokenError, 403 => EasyMeli::ForbiddenError, 500 => EasyMeli::InternalServerError, 501 => EasyMeli::NotImplementedError, 502 => EasyMeli::BadGatewayError, 503 => EasyMeli::ServiceUnavailableError, 504 => EasyMeli::GatewayTimeoutError }
Class Method Summary collapse
- .error_class(response) ⇒ Object
- .find_in_error_list(response_field) ⇒ Object
- .server_side_error?(response) ⇒ Boolean
- .status_code_listed?(response) ⇒ Boolean
- .status_error_class(response) ⇒ Object
Class Method Details
.error_class(response) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/easy_meli/error_parser.rb', line 23 def self.error_class(response) ERROR_FIELDS.each do |field| error = find_in_error_list(response[field]) return error if error end nil end |
.find_in_error_list(response_field) ⇒ Object
39 40 41 42 43 |
# File 'lib/easy_meli/error_parser.rb', line 39 def self.find_in_error_list(response_field) return if response_field.to_s.empty? ERROR_LIST.find { |key, _| response_field&.include?(key) }&.last end |
.server_side_error?(response) ⇒ Boolean
45 46 47 |
# File 'lib/easy_meli/error_parser.rb', line 45 def self.server_side_error?(response) response.code >= 500 end |
.status_code_listed?(response) ⇒ Boolean
49 50 51 |
# File 'lib/easy_meli/error_parser.rb', line 49 def self.status_code_listed?(response) STATUS_ERRORS.keys.include?(response.code) end |
.status_error_class(response) ⇒ Object
31 32 33 34 35 |
# File 'lib/easy_meli/error_parser.rb', line 31 def self.status_error_class(response) return unless self.status_code_listed?(response) || self.server_side_error?(response) STATUS_ERRORS[response.code] || EasyMeli::ServerError end |