Class: WishSimple::Response::RaiseError

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/wishsimple/response/raise_error.rb

Instance Method Summary collapse

Instance Method Details

#error_message(response) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/wishsimple/response/raise_error.rb', line 33

def error_message(response)
  if response[:body] && response[:body].is_a?(String)
    body = MultiJson.load(response[:body], symbolize_keys: true)
    body[:error] || ''
  else
    ''
  end
end

#on_complete(response) ⇒ Object



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

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