Exception: Api::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/api/errors.rb

Overview

All errors from this gem will inherit from this one.

Direct Known Subclasses

ClientError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



38
39
40
41
# File 'lib/api/errors.rb', line 38

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

Class Method Details

.from_response(response) ⇒ Api::Error

Returns the appropriate Api::Error subclass based on status and response message

Parameters:

  • response (Hash)

    HTTP response

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/api/errors.rb', line 10

def self.from_response(response)
  status = if response.respond_to?(:[])
             response[:status].to_i
           else
             response.status
           end

  if klass =  case status
              when 400      then Api::BadRequest
              when 401      then Api::Unauthorized
              when 403      then Api::Unauthorized
              when 404      then Api::NotFound
              when 405      then Api::MethodNotAllowed
              when 406      then Api::NotAcceptable
              when 409      then Api::Conflict
              when 415      then Api::UnsupportedMediaType
              when 422      then Api::UnprocessableEntity
              when 400..499 then Api::ClientError
              when 500      then Api::InternalServerError
              when 501      then Api::NotImplemented
              when 502      then Api::BadGateway
              when 503      then Api::ServiceUnavailable
              when 500..599 then Api::ServerError
              end
    klass.new(response)
  end
end