Class: Contactually::Middleware::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/contactually/middleware/error_middleware.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/contactually/middleware/error_middleware.rb', line 4

def call(env)
  @app.call(env).on_complete do |env|
    case env.status
      when 401
        # Missing or invalid auth token
        raise Contactually::UnauthorizedError.new(error_messages(env.response))
      when 402
        # Auth token could be invalid due to non-payment
        raise Contactually::PaymentRequiredError.new(error_messages(env.response))
      when 403
        # App does not have permission to perform the operation
        raise Contactually::ForbiddenError.new(error_messages(env.response))
      when 404
        # Standard REST resource not found or not allowed to fetch
        raise Contactually::NotFoundError.new(error_messages(env.response))
    end
  end
end