Class: GoCardlessPro::Middlewares::RaiseGoCardlessErrors

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/gocardless_pro/middlewares/raise_gocardless_errors.rb

Constant Summary collapse

API_ERROR_STATUSES =
501..599
CLIENT_ERROR_STATUSES =
400..500

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gocardless_pro/middlewares/raise_gocardless_errors.rb', line 7

def on_complete(env)
  if !json?(env) || API_ERROR_STATUSES.include?(env.status)
    raise ApiError, generate_error_data(env)
  end

  if CLIENT_ERROR_STATUSES.include?(env.status)
    json_body ||= JSON.parse(env.body) unless env.body.empty?
    error_type = json_body['error']['type']
    raise(error_class_for_type(error_type), json_body['error'])
  end
end