Class: IContact::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/icontact/errors.rb

Constant Summary collapse

MAPPED_ERRORS =
{
  400 => IContact::BadRequest,
  401 => IContact::NotAuthorized,
  402 => IContact::PaymentRequest,
  403 => IContact::Forbidden,
  405 => IContact::MethodNotAllowed,
  406 => IContact::NotAcceptable,
  415 => IContact::UnsupportedMediaType,
  500 => IContact::InternalServer,
  501 => IContact::NotImplemented,
  503 => IContact::ServiceUnavailable,
  507 => IContact::InsufficientSpace
}

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/icontact/errors.rb', line 43

def initialize(response)
  klass   = MAPPED_ERRORS[response.status.to_i]
  parsed  = Oj.load(response.body || '')
  message = parsed.nil? ? '' : parsed['errors']

  if klass
    raise klass.new(message)
  else
    raise IContactError.new("#{response.status}: #{message}")
  end
end