Exception: Assinafy::ApiError

Inherits:
Error
  • Object
show all
Defined in:
lib/assinafy/errors.rb,
sig/assinafy.rbs

Overview

Raised when the API responds with a non-2xx status, or with a 2xx response envelope whose embedded status code indicates failure.

The Assinafy v1 API returns two distinct error-body shapes, both handled here:

  • Framework errors: {"name":"Not Found","message":"Página não encontrada.","code":0,"status":404}
  • Application envelopes: {"status":404,"data":null,"message":"Template não encontrado."}

Examples:

Rescue an API error

begin
  client.documents.details('missing-id')
rescue Assinafy::ApiError => e
  e.status_code   # => 404
  e.message       # => "Documento não encontrado."
  e.error_name    # => "Not Found" (nil when the body omits it)
  e.error_code    # => 0          (nil when the body omits it)
  e.response_data # => the raw parsed body Hash
end

Instance Attribute Summary collapse

Attributes inherited from Error

#context

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status_code, response_data = nil) ⇒ ApiError

Returns a new instance of ApiError.

Parameters:

  • message (String)
  • status_code (Integer)
  • response_data (Object) (defaults to: nil)


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

def initialize(message, status_code, response_data = nil)
  super(message, { status_code: status_code, response_data: response_data })
  @status_code   = status_code
  @response_data = response_data
  return unless response_data.is_a?(Hash)

  @error_name = response_data['name']
  @error_code = response_data['code']
end

Instance Attribute Details

#error_codeInteger, ... (readonly)

Returns the API's code field, when present.

Returns:

  • (Integer, String, nil)

    the API's code field, when present



43
44
45
# File 'lib/assinafy/errors.rb', line 43

def error_code
  @error_code
end

#error_nameString? (readonly)

Returns the API's name field (framework errors only).

Returns:

  • (String, nil)

    the API's name field (framework errors only)



41
42
43
# File 'lib/assinafy/errors.rb', line 41

def error_name
  @error_name
end

#response_dataHash, ... (readonly)

Returns raw response body.

Returns:

  • (Hash, String, nil)

    raw response body



39
40
41
# File 'lib/assinafy/errors.rb', line 39

def response_data
  @response_data
end

#status_codeInteger (readonly)

Returns HTTP-style status code reported by the API.

Returns:

  • (Integer)

    HTTP-style status code reported by the API



37
38
39
# File 'lib/assinafy/errors.rb', line 37

def status_code
  @status_code
end

Class Method Details

.from_response(status_code, response_data) ⇒ ApiError

Build an Assinafy::ApiError from an HTTP status and parsed body. Reads the human message from message, error, or name (in that order).

Parameters:

  • status_code (Integer)
  • response_data (Hash, Object)

Returns:



61
62
63
64
65
# File 'lib/assinafy/errors.rb', line 61

def self.from_response(status_code, response_data)
  data = response_data.is_a?(Hash) ? response_data : {}
  message = data['message'] || data['error'] || data['name'] || 'API request failed'
  new(message.to_s, status_code, response_data)
end