class ReamazeAPI::Error

Encapsulates HTTP errors that may be returned by the Reamaze API. All API errors inherit from this class.

Public Class Methods

from_response(response) click to toggle source

Public: Create an exception from the given response.

response - HTTP response (Faraday::Env)

Returns a ReamazeAPI::Error or nil.

# File lib/reamaze_api/error.rb, line 10
def self.from_response(response)
  if klass = case response[:status].to_i
             when 403
               ReamazeAPI::Forbidden
             when 404
               ReamazeAPI::NotFound
             when 422
               ReamazeAPI::UnprocessableEntity
             when 429
               ReamazeAPI::TooManyRequests
             when 400..499
               ReamazeAPI::ClientError
             when 500..599
               ReamazeAPI::ServerError
             end

    klass.new(response)
  end
end
new(response = nil) click to toggle source

Public: Initialize a new ReamazeAPI::Error instance.

response - HTTP response (Faraday::Env)

Returns nothing.

Calls superclass method
# File lib/reamaze_api/error.rb, line 35
def initialize(response = nil)
  @response = response
  super(build_message)
end

Private Instance Methods

build_message() click to toggle source

Private: Error message to be displayed.

Returns a String or nil.

# File lib/reamaze_api/error.rb, line 45
def build_message
  return if @response.nil?

  message  = "#{@response[:method].to_s.upcase} "
  message << "#{@response[:url]}: "
  message << "#{@response[:status]}"
  message << "\n\nBODY: #{@response[:body].inspect}" if @response[:body]
  message
end