Exception: TurbaKit::Error

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

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, status) ⇒ Error

Returns a new instance of Error.

Raises:

  • (self)


25
26
27
28
29
30
31
32
33
# File 'lib/turba_kit/error.rb', line 25

def initialize(body, status)
  message = {
    status: status,
    error: body['error']
  }

  super(Oj.dump(message, mode: :compat)) 
  raise self
end

Class Method Details

.from_response(body, status) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/turba_kit/error.rb', line 3

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