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, nil) (defaults to: nil)

    Response body

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

    Response headers



87
88
89
90
91
92
# File 'lib/reve_ai/errors.rb', line 87

def initialize(message = nil, status: nil, body: nil, headers: nil)
  @status = status
  @body = body || {}
  @headers = headers || {}
  super(message)
end

Instance Attribute Details

#bodyHash (readonly)

Returns Response body parsed as Hash.

Returns:

  • (Hash)

    Response body parsed as Hash



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

def body
  @body
end

#headersHash (readonly)

Returns Response headers.

Returns:

  • (Hash)

    Response headers



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

def headers
  @headers
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 from the response body.

Returns:

  • (String, nil)

    Error code (e.g., “PROMPT_TOO_LONG”, “INVALID_API_KEY”)



106
107
108
# File 'lib/reve_ai/errors.rb', line 106

def error_code
  body[: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



99
100
101
# File 'lib/reve_ai/errors.rb', line 99

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