Exception: Nahook::APIError

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

Overview

Raised when the Nahook API returns an error response (4xx/5xx).

Examples:

Handling an API error

begin
  client.send("ep_123", payload: { order: 1 })
rescue Nahook::APIError => e
  puts e.status     # => 404
  puts e.code       # => "not_found"
  puts e.retryable? # => false
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, code, message, retry_after = nil) ⇒ APIError

Returns a new instance of APIError.

Parameters:

  • status (Integer)

    HTTP status code

  • code (String)

    machine-readable error code

  • message (String)

    human-readable error message

  • retry_after (Integer, nil) (defaults to: nil)

    Retry-After header value in seconds



31
32
33
34
35
36
# File 'lib/nahook/errors.rb', line 31

def initialize(status, code, message, retry_after = nil)
  @status = status
  @code = code
  @retry_after = retry_after
  super(message)
end

Instance Attribute Details

#codeString (readonly)

Returns machine-readable error code from the API.

Returns:

  • (String)

    machine-readable error code from the API



22
23
24
# File 'lib/nahook/errors.rb', line 22

def code
  @code
end

#retry_afterInteger? (readonly)

Returns seconds the client should wait before retrying.

Returns:

  • (Integer, nil)

    seconds the client should wait before retrying



25
26
27
# File 'lib/nahook/errors.rb', line 25

def retry_after
  @retry_after
end

#statusInteger (readonly)

Returns HTTP status code.

Returns:

  • (Integer)

    HTTP status code



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

def status
  @status
end

Instance Method Details

#auth_error?Boolean

Whether this is an authentication or authorization error.

Returns:

  • (Boolean)


48
49
50
# File 'lib/nahook/errors.rb', line 48

def auth_error?
  status == 401 || (status == 403 && code == "token_disabled")
end

#not_found?Boolean

Whether the requested resource was not found.

Returns:

  • (Boolean)


55
56
57
# File 'lib/nahook/errors.rb', line 55

def not_found?
  status == 404
end

#rate_limited?Boolean

Whether the request was rate limited.

Returns:

  • (Boolean)


62
63
64
# File 'lib/nahook/errors.rb', line 62

def rate_limited?
  status == 429
end

#retryable?Boolean

Whether this error is safe to retry (5xx or 429).

Returns:

  • (Boolean)


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

def retryable?
  status >= 500 || status == 429
end

#validation_error?Boolean

Whether the request failed validation.

Returns:

  • (Boolean)


69
70
71
# File 'lib/nahook/errors.rb', line 69

def validation_error?
  status == 400
end