Class: Vhx::Middleware::ErrorResponse

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/vhx/middleware/error_response.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



4
5
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/vhx/middleware/error_response.rb', line 4

def on_complete(env)
  error_class = case env[:status]
  when 200, 201, 204
  when 304
  when 400
    BadRequestError
  when 401
    if env[:body].fetch('message', '').match(/token/)
      InvalidTokenError
    else
      UnauthorizedError
    end
  when 402
    PaymentRequiredError
  when 404
    NotFoundError
  when 406
    NotAcceptableError
  else
    ServerError
  end

  if error_class
    raise error_class.new(env[:body], env[:status], env[:url])
  end
end