11# frozen_string_literal: true
22
33module Infisical
4- # Base class for all errors raised by this SDK.
4+ # Base class for all errors raised by this SDK. Rescue this to catch
5+ # anything Infisical-related.
56 class Error < StandardError ; end
67
7- # Raised when the Infisical API responds with a non-2xx status.
8+ # Raised when the Infisical API responds with a non-2xx status. Well-known
9+ # statuses raise a subclass ({AuthenticationError}, {PermissionError},
10+ # {NotFoundError}, {RateLimitError}, {ServerError}); rescuing this class
11+ # catches them all.
812 class APIError < Error
9- attr_reader :status , :url , :method , :request_id
13+ # @return [Integer] HTTP status code of the failed response
14+ attr_reader :status
1015
16+ # @return [String] full URL the failed request was sent to
17+ attr_reader :url
18+
19+ # @return [String] uppercase HTTP method of the failed request, e.g. "GET"
20+ attr_reader :method
21+
22+ # @return [String, nil] server-assigned request id, when the response carried one
23+ attr_reader :request_id
24+
25+ # Returns the error class that best matches an HTTP status, falling back
26+ # to {APIError} itself. Constants are resolved at call time, so the
27+ # subclasses defined below are visible here.
28+ #
29+ # @param status [Integer] HTTP status code
30+ # @return [Class<APIError>]
31+ def self . for_status ( status )
32+ case status
33+ when 401 then AuthenticationError
34+ when 403 then PermissionError
35+ when 404 then NotFoundError
36+ when 429 then RateLimitError
37+ when 500 ..599 then ServerError
38+ else self
39+ end
40+ end
41+
42+ # @param message [String] human-readable error detail from the API
43+ # @param status [Integer] HTTP status code
44+ # @param url [String] full URL the request was sent to
45+ # @param method [String] HTTP method of the request
46+ # @param request_id [String, nil] server-assigned request id, if any
1147 def initialize ( message , status :, url :, method :, request_id : nil )
1248 @status = status
1349 @url = url
@@ -21,9 +57,26 @@ def initialize(message, status:, url:, method:, request_id: nil)
2157 end
2258 end
2359
60+ # Raised on HTTP 401: missing, expired, or invalid credentials.
61+ class AuthenticationError < APIError ; end
62+
63+ # Raised on HTTP 403: the authenticated identity lacks permission for the
64+ # requested resource or action.
65+ class PermissionError < APIError ; end
66+
67+ # Raised on HTTP 404: the secret, project, environment, or path does not
68+ # exist (or is not visible to the authenticated identity).
69+ class NotFoundError < APIError ; end
70+
71+ # Raised on HTTP 429, after the client has exhausted its automatic retries.
72+ class RateLimitError < APIError ; end
73+
74+ # Raised on HTTP 5xx: the Infisical server failed to process the request.
75+ class ServerError < APIError ; end
76+
2477 # Raised when the request itself fails (network/timeout) before a
2578 # response is received. Always raised from inside a `rescue` for the
2679 # underlying network exception, so Ruby's built-in `Exception#cause`
27- # already carries it through — no need to track it ourselves.
80+ # already carries it through; no need to track it ourselves.
2881 class RequestError < Error ; end
2982end
0 commit comments