Exception: EasyPost::Errors::ApiError
- Inherits:
-
EasyPostError
- Object
- StandardError
- EasyPostError
- EasyPost::Errors::ApiError
- Defined in:
- lib/easypost/errors/api/api_error.rb
Direct Known Subclasses
BadRequestError, ConnectionError, ForbiddenError, GatewayTimeoutError, InternalServerError, InvalidRequestError, MethodNotAllowedError, NotFoundError, PaymentError, ProxyError, RateLimitError, RedirectError, RetryError, ServiceUnavailableError, SslError, TimeoutError, UnauthorizedError, UnknownApiError
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
Class Method Summary collapse
-
.collect_error_messages(error_message, messages_list) ⇒ Object
Recursively traverses a JSON element to extract error messages and returns them as a comma-separated string.
- .exception_cls_from_status_code(status_code) ⇒ Object
- .handle_api_error(response) ⇒ Object
Instance Method Summary collapse
-
#initialize(message, status_code = nil, error_code = nil, sub_errors = nil) ⇒ ApiError
constructor
A new instance of ApiError.
- #pretty_print ⇒ Object
Constructor Details
#initialize(message, status_code = nil, error_code = nil, sub_errors = nil) ⇒ ApiError
Returns a new instance of ApiError.
9 10 11 12 13 14 |
# File 'lib/easypost/errors/api/api_error.rb', line 9 def initialize(, status_code = nil, error_code = nil, sub_errors = nil) super @status_code = status_code @code = error_code @errors = sub_errors end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/easypost/errors/api/api_error.rb', line 7 def code @code end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/easypost/errors/api/api_error.rb', line 7 def errors @errors end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
7 8 9 |
# File 'lib/easypost/errors/api/api_error.rb', line 7 def status_code @status_code end |
Class Method Details
.collect_error_messages(error_message, messages_list) ⇒ Object
Recursively traverses a JSON element to extract error messages and returns them as a comma-separated string.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/easypost/errors/api/api_error.rb', line 25 def self.(, ) case when Hash .each_value { |value| (value, ) } when Array .each { |value| (value, ) } else .push(.to_s) end .join(', ') end |
.exception_cls_from_status_code(status_code) ⇒ Object
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/easypost/errors/api/api_error.rb', line 74 def self.exception_cls_from_status_code(status_code) case status_code when 0 EasyPost::Errors::ConnectionError when 300, 301, 302, 303, 304, 305, 306, 307, 308 EasyPost::Errors::RedirectError when 400 EasyPost::Errors::BadRequestError when 401 EasyPost::Errors::UnauthorizedError when 402 EasyPost::Errors::PaymentError when 403 EasyPost::Errors::ForbiddenError when 404 EasyPost::Errors::NotFoundError when 405 EasyPost::Errors::MethodNotAllowedError when 408 EasyPost::Errors::TimeoutError when 422 EasyPost::Errors::InvalidRequestError when 429 EasyPost::Errors::RateLimitError when 500 EasyPost::Errors::InternalServerError when 502, 504 EasyPost::Errors::GatewayTimeoutError when 503 EasyPost::Errors::ServiceUnavailableError else EasyPost::Errors::UnknownApiError end end |
.handle_api_error(response) ⇒ Object
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 |
# File 'lib/easypost/errors/api/api_error.rb', line 38 def self.handle_api_error(response) status_code = response.code status_code = status_code.to_i if status_code.is_a?(String) if status_code >= 200 && status_code <= 299 return nil end # Try to parse the response body as JSON begin error_data = JSON.parse(response.body)['error'] = error_data['message'] error_type = error_data['code'] errors = error_data['errors']&.map do |error| EasyPost::Models::Error.from_api_error_response(error) end rescue StandardError = response.code.to_s error_type = EasyPost::Constants::API_ERROR_DETAILS_PARSING_ERROR errors = nil end cls = exception_cls_from_status_code(status_code) if cls == EasyPost::Errors::UnknownApiError return EasyPost::Errors::UnknownApiError.new( EasyPost::Constants::UNEXPECTED_HTTP_STATUS_CODE % status_code, status_code, ) end # Return (don't throw here) an instance of the appropriate error class cls.new(, status_code, error_type, errors) end |
Instance Method Details
#pretty_print ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/easypost/errors/api/api_error.rb', line 16 def pretty_print error_string = "#{code} (#{status_code}): #{}" errors&.each do |error| error_string += "\nField: #{error.field}\nMessage: #{error.}" end error_string end |