Exception: Unleashed::Error

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

Overview

Custom error class for rescuing from all Unleashed errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil, errors_format = nil) ⇒ Error

Returns a new instance of Error.



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

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

Class Method Details

.from_response(response, errors_format = nil) ⇒ Unleashed::Error

Returns the appropriate Unleashed::Error subclass based on status

Parameters:

  • response (Faraday::Response)

    Faraday HTTP response

Returns:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/unleashed/error.rb', line 8

def self.from_response(response, errors_format = nil)
  klass = case response.status
          when 400      then Unleashed::BadRequest
          when 401      then Unleashed::Unauthorized
          when 403      then Unleashed::Forbidden
          when 404      then Unleashed::NotFound
          when 405      then Unleashed::MethodNotAllowed
          when 406      then Unleashed::NotAcceptable
          when 409      then Unleashed::Conflict
          when 422      then Unleashed::UnprocessableEntity
          when 400..499 then Unleashed::ClientError
          when 500      then Unleashed::InternalServerError
          when 501      then Unleashed::NotImplemented
          when 502      then Unleashed::BadGateway
          when 503      then Unleashed::ServiceUnavailable
          when 500..599 then Unleashed::ServerError
          end
  klass ? klass.new(response, errors_format) : new(response, errors_format)
end