Class: Response::RaiseReshapeApiError

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

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



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

def error_message(response)
  message = if (body = response[:body]) && !body.empty?
    if body.is_a?(String)
      body = MultiJson.load(body, :symbolize_keys => true)
    end
    ": #{body[:error] || body[:message] || ''}"
  else
    ''
  end
  "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}"
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
# File 'lib/faraday/response/raise_reshape_api_error.rb', line 6

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