Class: Afterpay::ErrorMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/afterpay/client.rb

Overview

Error middleware for Faraday to raise Afterpay connection errors

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorMiddleware

Returns a new instance of ErrorMiddleware.



57
58
59
# File 'lib/afterpay/client.rb', line 57

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/afterpay/client.rb', line 61

def call(env)
  @app.call(env).on_complete do
    case env[:status]
    when 404
      raise Client::NotFoundError, env.dig(:body, "message")
    when 401
      raise Client::UnauthorizedError, env.dig(:body, "message")
    end
  end
end