Exception: Nfe::Error

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

Overview

Base class for every error the SDK raises.

All SDK errors derive from Error, so consumer code can catch the whole family with a single rescue Nfe::Error. Errors derived from an HTTP response carry the response context (+status_code+, request_id, error_code, response_body, response_headers) for diagnostics.

#to_h returns a logging-safe Hash that deliberately omits the raw response body and headers, which may carry secrets or PII.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status_code: nil, request_id: nil, error_code: nil, response_body: nil, response_headers: {}) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String, nil) (defaults to: nil)

    human-readable message.

  • status_code (Integer, nil) (defaults to: nil)
  • request_id (String, nil) (defaults to: nil)
  • error_code (String, nil) (defaults to: nil)
  • response_body (String, nil) (defaults to: nil)
  • response_headers (Hash) (defaults to: {})


31
32
33
34
35
36
37
38
39
# File 'lib/nfe/errors.rb', line 31

def initialize(message = nil, status_code: nil, request_id: nil,
               error_code: nil, response_body: nil, response_headers: {})
  super(message)
  @status_code = status_code
  @request_id = request_id
  @error_code = error_code
  @response_body = response_body
  @response_headers = response_headers || {}
end

Instance Attribute Details

#error_codeString? (readonly)

Returns machine-readable error code from the body.

Returns:

  • (String, nil)

    machine-readable error code from the body.



19
20
21
# File 'lib/nfe/errors.rb', line 19

def error_code
  @error_code
end

#request_idString? (readonly)

Returns server-supplied request/correlation id.

Returns:

  • (String, nil)

    server-supplied request/correlation id.



17
18
19
# File 'lib/nfe/errors.rb', line 17

def request_id
  @request_id
end

#response_bodyString? (readonly)

Returns raw response body, for programmatic inspection.

Returns:

  • (String, nil)

    raw response body, for programmatic inspection.



21
22
23
# File 'lib/nfe/errors.rb', line 21

def response_body
  @response_body
end

#response_headersHash (readonly)

Returns response headers (lowercase keys).

Returns:

  • (Hash)

    response headers (lowercase keys).



23
24
25
# File 'lib/nfe/errors.rb', line 23

def response_headers
  @response_headers
end

#status_codeInteger? (readonly)

Returns HTTP status code, when derived from a response.

Returns:

  • (Integer, nil)

    HTTP status code, when derived from a response.



15
16
17
# File 'lib/nfe/errors.rb', line 15

def status_code
  @status_code
end

Instance Method Details

#to_hHash

Logging-safe representation. Never includes raw headers or body, which could leak the API key, certificate password, or PII.

Returns:

  • (Hash)


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

def to_h
  {
    type: self.class.name,
    message: message,
    status_code: status_code,
    request_id: request_id,
    error_code: error_code
  }
end