Exception: Bettery::Error

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

Overview

Custom error class for rescuing from all Betterplace errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



29
30
31
32
# File 'lib/bettery/error.rb', line 29

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

Class Method Details

.from_response(response) ⇒ Bettery::Error

Returns the appropriate Bettery::Error sublcass 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
# File 'lib/bettery/error.rb', line 10

def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s
  headers = response[:response_headers]

  if klass =  case status
              when 400      then Bettery::BadRequest
              when 403      then Bettery::Forbidden
              when 404      then Bettery::NotFound
              when 400..499 then Bettery::ClientError
              when 500      then Bettery::InternalServerError
              when 502      then Bettery::BadGateway
              when 503      then Bettery::ServiceUnavailable
              when 500..599 then Bettery::ServerError
              end
    klass.new(response)
  end
end