Exception: SeatLayer::APIError

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

Overview

Raised for any non-2xx response.

The API answers failures with "code":…, "message":… and a status. Surfacing that as one opaque exception leaves every caller string-matching on error. The subclasses below are the ones an integration actually branches on — a sold-out seat is a business outcome that belongs in a rescue of its own, not lumped in with a bad key.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, code:, body:, request_id:, message: nil) ⇒ APIError

Returns a new instance of APIError.



24
25
26
27
28
29
30
# File 'lib/seatlayer/errors.rb', line 24

def initialize(status:, code:, body:, request_id:, message: nil)
  @status = status
  @code = code
  @body = body
  @request_id = request_id
  super(message || "SeatLayer API error #{status} (#{code})")
end

Instance Attribute Details

#bodyHash (readonly)

Returns the decoded error body, for fields this SDK does not model.

Returns:

  • (Hash)

    the decoded error body, for fields this SDK does not model.



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

def body
  @body
end

#codeString (readonly)

Returns machine-readable slug: body "code", falling back to "error".

Returns:

  • (String)

    machine-readable slug: body "code", falling back to "error".



18
19
20
# File 'lib/seatlayer/errors.rb', line 18

def code
  @code
end

#request_idString? (readonly)

Returns correlation id from X-Request-ID. Quote it in support requests.

Returns:

  • (String, nil)

    correlation id from X-Request-ID. Quote it in support requests.



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

def request_id
  @request_id
end

#statusInteger (readonly)

Returns HTTP status the API answered with.

Returns:

  • (Integer)

    HTTP status the API answered with.



16
17
18
# File 'lib/seatlayer/errors.rb', line 16

def status
  @status
end

Class Method Details

.from_response(status:, body:, request_id:, retry_after:) ⇒ Object

Build the most specific error class for a response.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/seatlayer/errors.rb', line 33

def self.from_response(status:, body:, request_id:, retry_after:)
  code = presence(body["code"]) || presence(body["error"]) || "unknown_error"
  attrs = { status: status, code: code, body: body, request_id: request_id,
            message: presence(body["message"]) }

  case status
  when 401, 403 then AuthError.new(**attrs)
  when 404 then NotFoundError.new(**attrs)
  when 409 then ConflictError.new(**attrs)
  when 422 then ValidationError.new(**attrs)
  when 429 then RateLimitError.new(**attrs, retry_after: retry_after)
  else APIError.new(**attrs)
  end
end