class PayPoint::Blue::RaiseErrors

Faraday response middleware for handling various error scenarios

Public Instance Methods

on_complete(env) click to toggle source

Raise an error if the response outcome signifies a failure or the HTTP status code is 400 or greater.

@raise [Error::Validation] for an outcome code starting with V @raise [Error::Auth] for an outcome code starting with A @raise [Error::Cancelled] for an outcome code starting with C @raise [Error::External] for an outcome code starting with X @raise [Error::Suspended] for an outcome code starting with U @raise [Error::Client] for all other error scenarios

# File lib/paypoint/blue/raise_errors.rb, line 16
def on_complete(env)
  outcome = fetch_outcome(env)
  if outcome
    return if outcome[:reason_code] =~ /^S/
    fail error_from_outcome(outcome[:reason_code], response_values(env))
  elsif not_found?(env)
    fail Error::NotFound, response_values(env)
  elsif client_error?(env)
    fail Error::Client, response_values(env)
  end
end

Private Instance Methods

client_error?(env) click to toggle source
# File lib/paypoint/blue/raise_errors.rb, line 34
def client_error?(env)
  env.status >= 400
end
error_from_outcome(code, response_values) click to toggle source
# File lib/paypoint/blue/raise_errors.rb, line 42
def error_from_outcome(code, response_values)
  case code
  when /^V/ then Error::Validation.new(response_values)
  when /^A/ then Error::Auth.new(response_values)
  when /^C/ then Error::Cancelled.new(response_values)
  when /^X/ then Error::External.new(response_values)
  when /^U/ then Error::Suspended.new(response_values)
  else
    Error::Client.new(response_values)
  end
end
fetch_outcome(env) click to toggle source
# File lib/paypoint/blue/raise_errors.rb, line 38
def fetch_outcome(env)
  env.body.is_a?(Hash) && env.body[:outcome]
end
not_found?(env) click to toggle source
# File lib/paypoint/blue/raise_errors.rb, line 30
def not_found?(env)
  env.status == 404 && env.body[:reason_code] == "A400"
end
response_values(env) click to toggle source
# File lib/paypoint/blue/raise_errors.rb, line 54
def response_values(env)
  {
    status:  env.status,
    headers: env.response_headers,
    body:    env.body,
  }
end