Exception: Riveter::APIError

Inherits:
Error
  • Object
show all
Defined in:
lib/riveter/errors.rb

Overview

A non-2xx HTTP response, normalized across both error body dialects.

Constant Summary collapse

FALLBACK_TYPES =
{
  400 => "bad_request",
  401 => "unauthorized",
  403 => "forbidden",
  404 => "not_found",
  409 => "conflict",
  422 => "validation",
  429 => "rate_limited"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, type:, message:, details: nil) ⇒ APIError

Returns a new instance of APIError.



27
28
29
30
31
32
# File 'lib/riveter/errors.rb', line 27

def initialize(status:, type:, message:, details: nil)
  super(message)
  @status = status
  @type = type
  @details = details
end

Instance Attribute Details

#detailsObject (readonly)

Returns the value of attribute details.



25
26
27
# File 'lib/riveter/errors.rb', line 25

def details
  @details
end

#statusObject (readonly)

Returns the value of attribute status.



25
26
27
# File 'lib/riveter/errors.rb', line 25

def status
  @status
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/riveter/errors.rb', line 25

def type
  @type
end

Class Method Details

.from_response(status, body) ⇒ Object

Builds an APIError from either the clean { "error" => ... } envelope or the legacy { "request_status" => "error" } shape (401s).



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/riveter/errors.rb', line 36

def self.from_response(status, body)
  if body.is_a?(Hash)
    error = body["error"]
    if error.is_a?(Hash) && error["type"].is_a?(String)
      return new(
        status: status,
        type: error["type"],
        message: error["message"] || "HTTP #{status}",
        details: error["details"]
      )
    end
    if body["request_status"] == "error"
      return new(
        status: status,
        type: body["error_type"] || "error",
        message: body["message"] || "HTTP #{status}",
        details: body["errors"]
      )
    end
  end

  message = body.is_a?(String) && !body.empty? ? body[0, 200] : "HTTP #{status}"
  new(status: status, type: FALLBACK_TYPES[status] || "http_#{status}", message: message)
end