Exception: Infisical::APIError

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

Overview

Raised when the Infisical API responds with a non-2xx status. Well-known statuses raise a subclass (AuthenticationError, PermissionError, NotFoundError, RateLimitError, ServerError); rescuing this class catches them all.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, status:, url:, http_method:, request_id: nil) ⇒ APIError

Returns a new instance of APIError.

Parameters:

  • message (String)

    human-readable error detail from the API

  • status (Integer)

    HTTP status code

  • url (String)

    full URL the request was sent to

  • http_method (String)

    HTTP method of the request

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

    server-assigned request id, if any



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/infisical/errors.rb', line 47

def initialize(message, status:, url:, http_method:, request_id: nil)
  @status = status
  @url = url
  @http_method = http_method
  @request_id = request_id

  context = "[Method=#{http_method}] [URL=#{url}] [StatusCode=#{status}]"
  context += " [RequestId=#{request_id}]" if request_id

  super("#{context} #{message}")
end

Instance Attribute Details

#http_methodString (readonly)

Returns uppercase HTTP method of the failed request, e.g. "GET".

Returns:

  • (String)

    uppercase HTTP method of the failed request, e.g. "GET"



20
21
22
# File 'lib/infisical/errors.rb', line 20

def http_method
  @http_method
end

#request_idString? (readonly)

Returns server-assigned request id, when the response carried one.

Returns:

  • (String, nil)

    server-assigned request id, when the response carried one



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

def request_id
  @request_id
end

#statusInteger (readonly)

Returns HTTP status code of the failed response.

Returns:

  • (Integer)

    HTTP status code of the failed response



14
15
16
# File 'lib/infisical/errors.rb', line 14

def status
  @status
end

#urlString (readonly)

Returns full URL the failed request was sent to.

Returns:

  • (String)

    full URL the failed request was sent to



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

def url
  @url
end

Class Method Details

.for_status(status) ⇒ Class<APIError>

Returns the error class that best matches an HTTP status, falling back to Infisical::APIError itself. Constants are resolved at call time, so the subclasses defined below are visible here.

Parameters:

  • status (Integer)

    HTTP status code

Returns:



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

def self.for_status(status)
  case status
  when 401 then AuthenticationError
  when 403 then PermissionError
  when 404 then NotFoundError
  when 429 then RateLimitError
  when 500..599 then ServerError
  else self
  end
end