Exception: RunApi::Core::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/runapi/core/errors.rb

Overview

Base error class for all RunAPI SDK errors. Includes HTTP status, request ID, and response details.

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

Class Method Summary collapse

Instance Method Summary collapse

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(message = nil, status: nil, request_id: nil, details: nil)
  super(message)
  @status = status
  @request_id = request_id
  @details = details
end

Instance Attribute Details

#detailsHash, ... (readonly)

Returns Parsed response body or error details.

Returns:

  • (Hash, String, nil)

    Parsed response body or error details.



15
16
17
# File 'lib/runapi/core/errors.rb', line 15

def details
  @details
end

#request_idString? (readonly)

Returns Request ID from response headers.

Returns:

  • (String, nil)

    Request ID from response headers.



13
14
15
# File 'lib/runapi/core/errors.rb', line 13

def request_id
  @request_id
end

#statusInteger? (readonly)

Returns HTTP status code if available.

Returns:

  • (Integer, nil)

    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.

Parameters:

  • response (Net::HTTPResponse)

    HTTP response object

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

    Response body as string

Returns:

  • (Error)

    Specific error instance based on status code



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)
  message = extract_message(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(message, **kwargs)
end

Instance Method Details

#to_hObject



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: message,
    status: status,
    request_id: request_id,
    details: details
  }.compact
end