Class: Response::RaiseHttpError

Inherits:
Middleware
  • Object
show all
Defined in:
lib/faraday/response/raise_http_error.rb

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/faraday/response/raise_http_error.rb', line 31

def error_message(response)
  body = response[:body] || ''

  begin
    body = Hashie::Mash.new(JSON.parse(response[:body]))
  rescue
  end

  body.is_a?(::Hashie::Mash) ? body.error.description : body
end

#on_complete(response) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/faraday/response/raise_http_error.rb', line 6

def on_complete(response)
  case response[:status].to_i
  when 400
    raise Lelylan::BadRequest, error_message(response)
  when 401
    raise Lelylan::Unauthorized, error_message(response)
  when 403
    raise Lelylan::Forbidden, error_message(response)
  when 404
    raise Lelylan::NotFound, error_message(response)
  when 406
    raise Lelylan::NotAcceptable, error_message(response)
  when 422
    raise Lelylan::UnprocessableEntity, error_message(response)
  when 500
    raise Lelylan::InternalServerError, error_message(response)
  when 501
    raise Lelylan::NotImplemented, error_message(response)
  when 502
    raise Lelylan::BadGateway, error_message(response)
  when 503
    raise Lelylan::ServiceUnavailable, error_message(response)
  end
end