class Shipcloud::ShipcloudError

Attributes

errors[R]
response[R]

Public Class Methods

from_response(response) click to toggle source

Returns the appropriate Shipcloud::ShipcloudError subclass based on status code

@param [HTTPResponse] response HTTP response @return [Shipcloud::ShipcloudError]

# File lib/shipcloud/shipcloud_error.rb, line 19
def self.from_response(response)
  response_code = response.code.to_i
  if error_class = error_class_for(response_code)
    error_class.new(response)
  end
end
new(response = nil) click to toggle source
Calls superclass method
# File lib/shipcloud/shipcloud_error.rb, line 7
def initialize(response = nil)
  @response = response
  @errors = parse_errors
  error_message = errors.empty? ? response_body : errors
  super(error_message)
end

Private Class Methods

error_class_for(response_code) click to toggle source
# File lib/shipcloud/shipcloud_error.rb, line 26
def self.error_class_for(response_code)
  case response_code
  when 400, 422 then InvalidRequestError
  when 401 then AuthenticationError
  when 402 then TooManyRequests
  when 403 then ForbiddenError
  when 404 then NotFoundError
  when 400..499 then ClientError
  when 500..599 then ServerError
  end
end

Private Instance Methods

parse_errors() click to toggle source
# File lib/shipcloud/shipcloud_error.rb, line 42
def parse_errors
  return [] unless response_body

  data = JSON.parse(response_body)
  if data.is_a?(Hash) && data["errors"]
    data["errors"]
  else
    []
  end
rescue JSON::ParserError
  []
end
response_body() click to toggle source
# File lib/shipcloud/shipcloud_error.rb, line 55
def response_body
  return unless @response

  @response.body
end