Class: GoCardlessPro::Middlewares::RaiseGoCardlessErrors

Inherits:
Faraday::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
API_STATUS_NO_CONTENT =
204

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

Raises:

  • (error_class)


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

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

  return unless CLIENT_ERROR_STATUSES.include?(env.status)

  json_body ||= JSON.parse(env.body) unless env.body.empty?
  error_type = json_body['error']['type']

  error_class = error_class_for_status(env.status) || error_class_for_type(error_type)

  raise(error_class, json_body['error'])
end