Class: Polygonio::ErrorMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/polygonio/rest/errors.rb

Constant Summary collapse

CLIENT_ERROR_STATUSES =
(400...500).freeze
SERVER_ERROR_STATUSES =
(500...600).freeze

Instance Method Summary collapse

Instance Method Details

#call(request_env) ⇒ Object



39
40
41
# File 'lib/polygonio/rest/errors.rb', line 39

def call(request_env)
  @app.call(request_env).on_complete(&method(:on_complete))
end

#on_complete(response_env) ⇒ Object

rubocop:disable Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/polygonio/rest/errors.rb', line 20

def on_complete(response_env) # rubocop:disable Metrics/MethodLength
  status = response_env.status

  case status
  when 400
    raise Errors::BadRequestError
  when 401
    raise Errors::UnauthorizedError
  when 403
    raise Errors::ForbiddenError
  when 404
    raise Errors::ResourceNotFoundError
  when CLIENT_ERROR_STATUSES
    raise Errors::UnknownError
  when SERVER_ERROR_STATUSES
    raise Errors::ServerError
  end
end