Exception: Ipregistry::ApiError

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

Overview

Raised when the Ipregistry API reports a failure, such as an invalid IP address, an exhausted credit balance, or throttling.

Each documented error code maps to a dedicated subclass (for example INVALID_API_KEY raises InvalidApiKeyError), so callers can rescue the exact condition they care about. The raw code, the suggested resolution, and the HTTP status remain available on every instance.

In batch lookups, an ApiError may also describe the failure of a single entry rather than the whole request (see BatchResponse); such per-entry errors are returned, not raised.

The full list of error codes is documented at https://ipregistry.co/docs/errors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = "API error", code: nil, resolution: nil, http_status: nil) ⇒ ApiError

Returns a new instance of ApiError.



53
54
55
56
57
58
# File 'lib/ipregistry/errors.rb', line 53

def initialize(message = "API error", code: nil, resolution: nil, http_status: nil)
  @code = code
  @resolution = resolution
  @http_status = http_status
  super(message)
end

Instance Attribute Details

#codeString? (readonly)

Returns the raw error code returned by the API.

Returns:

  • (String, nil)

    the raw error code returned by the API



45
46
47
# File 'lib/ipregistry/errors.rb', line 45

def code
  @code
end

#http_statusInteger? (readonly)

Returns the HTTP status of the response, when known.

Returns:

  • (Integer, nil)

    the HTTP status of the response, when known



51
52
53
# File 'lib/ipregistry/errors.rb', line 51

def http_status
  @http_status
end

#resolutionString? (readonly)

Returns a suggestion on how to resolve the error.

Returns:

  • (String, nil)

    a suggestion on how to resolve the error



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

def resolution
  @resolution
end

Class Method Details

.from_payload(payload, http_status: nil) ⇒ ApiError

Builds the most specific error for a decoded API error payload.

Parameters:

  • payload (Hash)

    the decoded error body ("code", "message", "resolution")

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

    the HTTP status of the response

Returns:

  • (ApiError)

    an instance of the subclass matching the error code, or of ApiError itself when the code is not recognized



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ipregistry/errors.rb', line 66

def self.from_payload(payload, http_status: nil)
  payload = {} unless payload.is_a?(Hash)
  code = payload["code"]
  klass = ERRORS_BY_CODE.fetch(code, ApiError)
  klass.new(
    payload["message"] || "API error",
    code: code,
    resolution: payload["resolution"],
    http_status: http_status
  )
end