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",
  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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, code: nil, status: nil, request_id: nil, details: nil, response_headers: nil) ⇒ Error

Returns a new instance of Error.



21
22
23
24
25
26
27
28
# File 'lib/runapi/core/errors.rb', line 21

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

#codeString? (readonly)

Returns Explicit machine-readable reason.

Returns:

  • (String, nil)

    Explicit machine-readable reason.



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

def code
  @code
end

#detailsHash, ... (readonly)

Returns Parsed response body or error details.

Returns:

  • (Hash, String, nil)

    Parsed response body or error details.



17
18
19
# File 'lib/runapi/core/errors.rb', line 17

def details
  @details
end

#request_idString? (readonly)

Returns Request ID from response headers.

Returns:

  • (String, nil)

    Request ID from response headers.



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

def request_id
  @request_id
end

#response_headersResponseHeaders (readonly)

Returns HTTP response headers when available.

Returns:



19
20
21
# File 'lib/runapi/core/errors.rb', line 19

def response_headers
  @response_headers
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



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
111
112
113
114
115
# File 'lib/runapi/core/errors.rb', line 86

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)
  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 = {
    code: extract_code(parsed_body),
    status: status,
    request_id: request_id,
    details: parsed_body,
    response_headers: headers
  }
  kwargs[:retry_after] = retry_after if error_class == RateLimitError

  error_class.new(message, **kwargs)
end

Instance Method Details

#response_header(name) ⇒ Object



30
31
32
# File 'lib/runapi/core/errors.rb', line 30

def response_header(name)
  response_headers[name]
end

#runapi_task_idObject



34
35
36
# File 'lib/runapi/core/errors.rb', line 34

def runapi_task_id
  response_header("X-RunAPI-Task-Id")
end

#to_hObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/runapi/core/errors.rb', line 38

def to_h
  {
    error: self.class.name,
    message: message,
    code: code,
    status: status,
    request_id: request_id,
    details: details
  }.compact
end