Exception: Dimelo::CCP::API::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/dimelo/ccp/api/error.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#original_exceptionObject

Returns the value of attribute original_exception.



4
5
6
# File 'lib/dimelo/ccp/api/error.rb', line 4

def original_exception
  @original_exception
end

Class Method Details

.from(method, path, http_status, body) ⇒ Object

Public : Return specific Exceptions if defined

Returns :

  • DefinedError descendant exception (DomainNotFoundError,…) if status & name matches declared DefinedError child

  • BaseError if status and name does not match any declared Error

  • Error if body cannot be json parsed



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dimelo/ccp/api/error.rb', line 12

def self.from(method, path, http_status, body)
  json = Dimelo::CCP::API.decode_json(body).symbolize_keys!
  name = json.delete(:error)
  status = json.delete(:status)
  message = json.delete(:message)

  if klass = DefinedError.descendants.find { |error| error.status == status && error.name == name}
    klass.new
  else
    BaseError.new(name, status, message)
  end
rescue ActiveSupport::JSON.parse_error
  new("#{method.to_s.upcase} #{path} - #{http_status} #{body}")
end