Exception: ReamazeAPI::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/reamaze_api/error.rb

Overview

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

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ void

Initialize a new ReamazeAPI::Error instance.

Parameters:

  • response (defaults to: nil)

    HTTP response (Faraday::Env)



35
36
37
38
# File 'lib/reamaze_api/error.rb', line 35

def initialize(response = nil)
  @response = response
  super(build_message)
end

Class Method Details

.from_response(response) ⇒ Object

Create an exception from the given response.

Parameters:

  • response

    HTTP response (Faraday::Env)

Returns:

  • a ReamazeAPI::Error or nil.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#build_messageObject (private)

Private: Error message to be displayed.

Returns:

  • a String or nil.



45
46
47
48
49
50
51
52
53
# 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