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", 500 => "ServerError", 501 => "ServerError", 502 => "ServerError", 503 => "ServiceUnavailableError", 504 => "ServerError", 505 => "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", 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.
-
#response_headers ⇒ ResponseHeaders
readonly
HTTP response headers when available.
-
#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, response_headers: nil) ⇒ Error
constructor
A new instance of Error.
- #response_header(name) ⇒ Object
- #runapi_task_id ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(message = nil, status: nil, request_id: nil, details: nil, response_headers: nil) ⇒ Error
Returns a new instance of Error.
19 20 21 22 23 24 25 |
# File 'lib/runapi/core/errors.rb', line 19 def initialize( = nil, status: nil, request_id: nil, details: nil, response_headers: nil) super() @status = status @request_id = request_id @details = details @response_headers = response_headers.is_a?(ResponseHeaders) ? response_headers : ResponseHeaders.new(response_headers) 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 |
#response_headers ⇒ ResponseHeaders (readonly)
Returns HTTP response headers when available.
17 18 19 |
# File 'lib/runapi/core/errors.rb', line 17 def response_headers @response_headers 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.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/runapi/core/errors.rb', line 82 def from_response(response, body = nil) status = response.code.to_i request_id = response["x-request-id"] headers = response_headers(response) 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, response_headers: headers } kwargs[:retry_after] = retry_after if error_class == RateLimitError error_class.new(, **kwargs) end |
Instance Method Details
#response_header(name) ⇒ Object
27 28 29 |
# File 'lib/runapi/core/errors.rb', line 27 def response_header(name) response_headers[name] end |
#runapi_task_id ⇒ Object
31 32 33 |
# File 'lib/runapi/core/errors.rb', line 31 def runapi_task_id response_header("X-RunAPI-Task-Id") end |
#to_h ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/runapi/core/errors.rb', line 35 def to_h { error: self.class.name, message: , status: status, request_id: request_id, details: details }.compact end |