Exception: VisionAPI::APIError

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

Overview

A structured failure from the API.

Every failure carries the same envelope — {"error": {code, message, details?}} — and code is contract while message is prose that changes. Rescue the class you mean, or switch on code; never branch on the message text.

Constant Summary collapse

RETRYABLE_CODES =

"too_many_tasks" is 429 but absent on purpose: it clears when the caller's own task finishes, so an automatic sleep-and-retry would block the very thing it waits for.

%w[rate_limited internal_error provider_error].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, code:, message:, details: nil, headers: nil) ⇒ APIError

Returns a new instance of APIError.



34
35
36
37
38
39
40
41
# File 'lib/vision_api/errors.rb', line 34

def initialize(status:, code:, message:, details: nil, headers: nil)
  super("#{code}: #{message}")
  @status = status
  @code = code
  @details = details || {}
  @headers = headers || {}
  @request_id = @details["request_id"] || @headers["x-request-id"]
end

Instance Attribute Details

#codeString (readonly)

Returns the stable machine-readable value from the envelope.

Returns:

  • (String)

    the stable machine-readable value from the envelope



25
26
27
# File 'lib/vision_api/errors.rb', line 25

def code
  @code
end

#detailsHash (readonly)

Returns whatever the endpoint attached: +required+/+available+ on 402, request_id on 500, max_pages on 413.

Returns:

  • (Hash)

    whatever the endpoint attached: +required+/+available+ on 402, request_id on 500, max_pages on 413



28
29
30
# File 'lib/vision_api/errors.rb', line 28

def details
  @details
end

#headersHash (readonly)

Returns the response headers, lower-cased.

Returns:

  • (Hash)

    the response headers, lower-cased



30
31
32
# File 'lib/vision_api/errors.rb', line 30

def headers
  @headers
end

#request_idString? (readonly)

Returns the correlation id. Quote it in support requests.

Returns:

  • (String, nil)

    the correlation id. Quote it in support requests.



32
33
34
# File 'lib/vision_api/errors.rb', line 32

def request_id
  @request_id
end

#statusInteger (readonly)

Returns the HTTP status.

Returns:

  • (Integer)

    the HTTP status



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

def status
  @status
end

Instance Method Details

#retryable?Boolean

Whether the identical call could plausibly succeed on a second attempt. False for every input error, and for insufficient_credits — no amount of retrying changes a balance.

Returns:

  • (Boolean)


46
47
48
# File 'lib/vision_api/errors.rb', line 46

def retryable?
  RETRYABLE_CODES.include?(code)
end