Exception: RunApi::Core::Error
- Inherits:
-
StandardError
- Object
- StandardError
- RunApi::Core::Error
- Defined in:
- lib/runapi/core/errors.rb
Overview
Base error class for all RunAPI SDK errors. Includes HTTP status, request ID, and response details.
Direct Known Subclasses
AuthenticationError, ConflictError, InsufficientCreditsError, NetworkError, NotFoundError, RateLimitError, ServerError, ServiceUnavailableError, TaskFailedError, TaskTimeoutError, TimeoutError, ValidationError
Constant Summary collapse
- STATUS_MAP =
{ 400 => "ValidationError", 401 => "AuthenticationError", 402 => "InsufficientCreditsError", 404 => "NotFoundError", 409 => "ConflictError", 422 => "ValidationError", 429 => "RateLimitError", 451 => "ValidationError", 455 => "ServiceUnavailableError", 500 => "ServerError", 501 => "ServerError", 502 => "ServerError", 503 => "ServiceUnavailableError", 504 => "ServerError", 505 => "ServerError", 531 => "ServerError" }.freeze
- DEFAULT_MESSAGES =
{ 400 => "Bad request", 401 => "Unauthorized", 402 => "Insufficient credits", 404 => "Not found", 408 => "Request timeout", 409 => "Conflict", 413 => "Payload too large", 415 => "Unsupported media type", 422 => "Validation failed", 429 => "Too many requests", 451 => "Failed to fetch image", 455 => "Service unavailable (maintenance)", 503 => "Service unavailable" }.freeze
Instance Attribute Summary collapse
-
#details ⇒ Hash, ...
readonly
Parsed response body or error details.
-
#request_id ⇒ String?
readonly
Request ID from response headers.
-
#status ⇒ Integer?
readonly
HTTP status code if available.
Class Method Summary collapse
-
.from_response(response, body = nil) ⇒ Error
Constructs appropriate error class from HTTP response.
Instance Method Summary collapse
-
#initialize(message = nil, status: nil, request_id: nil, details: nil) ⇒ Error
constructor
A new instance of Error.
- #to_h ⇒ Object
Constructor Details
#initialize(message = nil, status: nil, request_id: nil, details: nil) ⇒ Error
Returns a new instance of Error.
17 18 19 20 21 22 |
# File 'lib/runapi/core/errors.rb', line 17 def initialize( = nil, status: nil, request_id: nil, details: nil) super() @status = status @request_id = request_id @details = details end |
Instance Attribute Details
#details ⇒ Hash, ... (readonly)
Returns Parsed response body or error details.
15 16 17 |
# File 'lib/runapi/core/errors.rb', line 15 def details @details end |
#request_id ⇒ String? (readonly)
Returns Request ID from response headers.
13 14 15 |
# File 'lib/runapi/core/errors.rb', line 13 def request_id @request_id end |
#status ⇒ Integer? (readonly)
Returns HTTP status code if available.
11 12 13 |
# File 'lib/runapi/core/errors.rb', line 11 def status @status end |
Class Method Details
.from_response(response, body = nil) ⇒ Error
Constructs appropriate error class from HTTP response. Maps status codes to specific error types and extracts error messages.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/runapi/core/errors.rb', line 76 def from_response(response, body = nil) status = response.code.to_i request_id = response["x-request-id"] parsed_body = parse_body(body) = (parsed_body) || DEFAULT_MESSAGES[status] || "Request failed" retry_after = parse_retry_after(response["retry-after"]) error_class_name = STATUS_MAP[status] error_class = if error_class_name Core.const_get(error_class_name) else Error end kwargs = { status: status, request_id: request_id, details: parsed_body } kwargs[:retry_after] = retry_after if error_class == RateLimitError error_class.new(, **kwargs) end |
Instance Method Details
#to_h ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/runapi/core/errors.rb', line 24 def to_h { error: self.class.name, message: , status: status, request_id: request_id, details: details }.compact end |