Exception: Ipregistry::ApiError
- 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
Direct Known Subclasses
BadRequestError, DisabledApiKeyError, ForbiddenIpError, ForbiddenIpOriginError, ForbiddenOriginError, InsufficientCreditsError, InternalError, InvalidApiKeyError, InvalidAsnError, InvalidFilterSyntaxError, InvalidIpAddressError, MissingApiKeyError, ReservedAsnError, ReservedIpAddressError, TooManyAsnsError, TooManyIpsError, TooManyRequestsError, TooManyUserAgentsError, UnknownAsnError
Instance Attribute Summary collapse
-
#code ⇒ String?
readonly
The raw error code returned by the API.
-
#http_status ⇒ Integer?
readonly
The HTTP status of the response, when known.
-
#resolution ⇒ String?
readonly
A suggestion on how to resolve the error.
Class Method Summary collapse
-
.from_payload(payload, http_status: nil) ⇒ ApiError
Builds the most specific error for a decoded API error payload.
Instance Method Summary collapse
-
#initialize(message = "API error", code: nil, resolution: nil, http_status: nil) ⇒ ApiError
constructor
A new instance of ApiError.
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( = "API error", code: nil, resolution: nil, http_status: nil) @code = code @resolution = resolution @http_status = http_status super() end |
Instance Attribute Details
#code ⇒ String? (readonly)
Returns the raw error code returned by the API.
45 46 47 |
# File 'lib/ipregistry/errors.rb', line 45 def code @code end |
#http_status ⇒ Integer? (readonly)
Returns the HTTP status of the response, when known.
51 52 53 |
# File 'lib/ipregistry/errors.rb', line 51 def http_status @http_status end |
#resolution ⇒ String? (readonly)
Returns 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.
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 |