Exception: Faraday::HttpError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/faraday/http_error.rb,
lib/faraday/http_error/version.rb,
lib/faraday/http_error/raise_error.rb

Direct Known Subclasses

ClientError, ServerError

Defined Under Namespace

Classes: BadRequest, ClientError, Forbidden, InternalServerError, NotFound, RaiseError, ServerError, ServiceUnavailable, UnprocessableEntity

Constant Summary collapse

VERSION =
'0.0.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = nil) ⇒ HttpError



8
9
10
11
12
13
14
# File 'lib/faraday/http_error.rb', line 8

def initialize(body = nil)
  @body = body
  message = "#{body}\n#{self.class.name} - " \
    "version #{Faraday::HttpError::VERSION}"

  super(message)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/faraday/http_error.rb', line 6

def body
  @body
end

Class Method Details

.class_error_for_status(http_status) ⇒ Object



26
27
28
29
30
31
# File 'lib/faraday/http_error.rb', line 26

def self.class_error_for_status(http_status)
  case http_status
  when 400..499 then client_errors(http_status)
  when 500..599 then server_errors(http_status)
  end
end

.client_errors(http_status) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/faraday/http_error.rb', line 33

def self.client_errors(http_status)
  case http_status
  when 400      then BadRequest
  when 403      then Forbidden
  when 404      then NotFound
  when 422      then UnprocessableEntity
  when 400..499 then ClientError
  end
end

.from_response(response) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/faraday/http_error.rb', line 16

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

  if (klass = class_error_for_status(status))
    # TODO: improve the error message
    klass.new(body)
  end
end

.server_errors(http_status) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/faraday/http_error.rb', line 43

def self.server_errors(http_status)
  case http_status
  when 500      then InternalServerError
  when 503      then ServiceUnavailable
  when 500..599 then ServerError
  end
end