Class: NOWPayments::Middleware::ErrorHandler

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/nowpayments/middleware/error_handler.rb

Overview

Faraday middleware that converts HTTP errors into NOWPayments exceptions

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/nowpayments/middleware/error_handler.rb', line 24

def call(env)
  @app.call(env).on_complete do |response_env|
    on_complete(response_env)
  end
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  raise ConnectionError, e.message
end

#on_complete(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nowpayments/middleware/error_handler.rb', line 9

def on_complete(env)
  case env[:status]
  when 400
    raise BadRequestError, env
  when 401, 403
    raise AuthenticationError, env
  when 404
    raise NotFoundError, env
  when 429
    raise RateLimitError, env
  when 500..599
    raise ServerError, env
  end
end