class FriendlyShipping::Services::UpsFreight::RestfulApiErrorHandler

Public Class Methods

call(error, original_request: nil, original_response: nil) click to toggle source
# File lib/friendly_shipping/services/ups_freight/restful_api_error_handler.rb, line 9
def self.call(error, original_request: nil, original_response: nil)
  parsed_json = JSON.parse(error.response.body)
  errors = parsed_json.dig('response', 'errors')

  failure_string = errors.map do |err|
    status = err['code']
    desc = err['message']
    [status, desc].compact.join(": ").presence || 'UPS could not process the request.'
  end.join("\n")

  Failure(
    ApiFailure.new(
      failure_string,
      original_request: original_request,
      original_response: original_response
    )
  )
end