Exception: ReveAI::APIError

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

Overview

Base class for API errors with HTTP status and response details.

All API-related exceptions inherit from this class and include HTTP status code, response body, and headers for debugging.

Examples:

Handling API errors

begin
  client.images.create(prompt: "...")
rescue ReveAI::UnauthorizedError => e
  puts "Auth failed: #{e.message}"
  puts "Status: #{e.status}"
rescue ReveAI::RateLimitError => e
  puts "Rate limited, retry after #{e.retry_after} seconds"
rescue ReveAI::APIError => e
  puts "API error (#{e.status}): #{e.message}"
  puts "Request ID: #{e.request_id}"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, status: nil, body: nil, headers: nil) ⇒ APIError

Creates a new API error instance.

Parameters:

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

    Error message

  • status (Integer, nil) (defaults to: nil)

    HTTP status code

  • body (Hash, String, nil) (defaults to: nil)

    Response body

  • headers (Hash, nil) (defaults to: nil)

    Response headers



91
92
93
94
95
96
97
# File 'lib/reve_ai/errors.rb', line 91

def initialize(message = nil, status: nil, body: nil, headers: nil)
  @status = status
  @body = body || {}
  @headers = headers || {}
  @params = @body.is_a?(Hash) ? @body[:params] : nil
  super(message)
end

Instance Attribute Details

#bodyHash, String (readonly)

Returns Response body parsed as Hash, or raw String for binary error responses (e.g., grey image bodies).

Returns:

  • (Hash, String)

    Response body parsed as Hash, or raw String for binary error responses (e.g., grey image bodies)



77
78
79
# File 'lib/reve_ai/errors.rb', line 77

def body
  @body
end

#headersHash (readonly)

Returns Response headers.

Returns:

  • (Hash)

    Response headers



80
81
82
# File 'lib/reve_ai/errors.rb', line 80

def headers
  @headers
end

#paramsHash? (readonly)

Returns Additional error details from the response body.

Returns:

  • (Hash, nil)

    Additional error details from the response body



83
84
85
# File 'lib/reve_ai/errors.rb', line 83

def params
  @params
end

#statusInteger? (readonly)

Returns HTTP status code.

Returns:

  • (Integer, nil)

    HTTP status code



73
74
75
# File 'lib/reve_ai/errors.rb', line 73

def status
  @status
end

Instance Method Details

#error_codeString?

Returns the error code for this error.

Read from the response body when present, falling back to the X-Reve-Error-Code header: with an image Accept header, the API answers errors with a small grey image body and no JSON error code.

Returns:

  • (String, nil)

    Error code (e.g., "PROMPT_TOO_LONG", "INVALID_API_KEY")



115
116
117
118
119
# File 'lib/reve_ai/errors.rb', line 115

def error_code
  return body[:error_code] if body.is_a?(Hash) && body[:error_code]

  headers["x-reve-error-code"]
end

#request_idString?

Returns the request ID from response headers.

Useful for debugging and support requests.

Returns:

  • (String, nil)

    Request ID if present



104
105
106
# File 'lib/reve_ai/errors.rb', line 104

def request_id
  headers["x-reve-request-id"]
end