Class: Spice::Response::ClientError

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

Instance Method Summary collapse

Instance Method Details

#error(body) ⇒ Object


24
25
26
27
28
29
30
# File 'lib/spice/response/client_error.rb', line 24

def error(body)
  if body["error"].kind_of?(Array)
    body["error"].join(',')
  else
    body["error"]
  end
end

#on_complete(env) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spice/response/client_error.rb', line 7

def on_complete(env)
  case env[:status].to_i
  when 400
    raise Spice::Error::BadRequest.new(error(env[:body]), env[:request_headers])
  when 401
    raise Spice::Error::Unauthorized.new(error(env[:body]), env[:request_headers])
  when 403
    raise Spice::Error::Forbidden.new(error(env[:body]), env[:request_headers])
  when 404
    raise Spice::Error::NotFound.new(error(env[:body]), env[:request_headers])
  when 406
    raise Spice::Error::NotAcceptable.new(error(env[:body]), env[:request_headers])
  when 409
    raise Spice::Error::Conflict.new(error(env[:body]), env[:request_headers])
  end
end