Exception: Anthropic::Errors::APIStatusError

Inherits:
APIError
  • Object
show all
Defined in:
lib/anthropic/errors.rb,
sig/anthropic/errors.rbs

Instance Attribute Summary collapse

Attributes inherited from APIError

#body, #headers, #url

Attributes inherited from Error

#cause

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from APIError

#request_id, #workspace_id

Constructor Details

#initialize(url:, status:, headers:, body:, request:, response:, message: nil) ⇒ APIStatusError

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of APIStatusError.

Parameters:

  • url (URI::Generic)
  • status (Integer)
  • headers (Hash{String=>String}, nil)
  • body (Object, nil)
  • request (nil)
  • response (nil)
  • message (String, nil) (defaults to: nil)
  • url: (URI::Generic)
  • status: (Integer)
  • headers: (::Hash[String, String], nil)
  • body: (Object, nil)
  • request: (nil)
  • response: (nil)
  • message: (String, nil) (defaults to: nil)


229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/anthropic/errors.rb', line 229

def initialize(url:, status:, headers:, body:, request:, response:, message: nil)
  message ||= {url: url.to_s, status: status, body: body}
  super(
    url: url,
    status: status,
    headers: headers,
    body: body,
    request: request,
    response: response,
    message: message&.to_s
  )
end

Instance Attribute Details

#status ⇒ Integer

Returns:

  • (Integer)


# File 'lib/anthropic/errors.rb', line 216

Class Method Details

.for(url:, status:, headers:, body:, request:, response:, message: nil) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • url (URI::Generic)
  • status (Integer)
  • headers (Hash{String=>String}, nil)
  • body (Object, nil)
  • request (nil)
  • response (nil)
  • message (String, nil) (defaults to: nil)
  • url: (URI::Generic)
  • status: (Integer)
  • headers: (::Hash[String, String], nil)
  • body: (Object, nil)
  • request: (nil)
  • response: (nil)
  • message: (String, nil) (defaults to: nil)

Returns:

  • (self)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/anthropic/errors.rb', line 197

def self.for(url:, status:, headers:, body:, request:, response:, message: nil)
  kwargs =
    {
      url: url,
      status: status,
      headers: headers,
      body: body,
      request: request,
      response: response,
      message: message
    }

  error_code = Anthropic::Internal::Util.dig(body, [:error, :details, :error_code])
  error_class = Anthropic::Errors::ERROR_CLASSES[[status, error_code]]
  error_class ||= Anthropic::Errors::ERROR_CLASSES[[status, nil]]
  error_class ||= status >= 500 ? Anthropic::Errors::InternalServerError : Anthropic::Errors::APIStatusError
  error_class.new(**kwargs)
end

Instance Method Details

#`type` ⇒ Anthropic::Models::error_type?

Returns:

  • (Anthropic::Models::error_type, nil)


73
# File 'sig/anthropic/errors.rbs', line 73

def `type`: -> Anthropic::Models::error_type?

#api_message ⇒ String?

A human-readable explanation of the error. Its wording can change, so don't match on it.

error.message from the response body, if present.

Returns:

  • (String, nil)


150
# File 'lib/anthropic/errors.rb', line 150

def api_message = Anthropic::Internal::Util.dig(body, [:error, :message])

#details ⇒ Anthropic::Models::ErrorDetails?

Machine-readable detail about the cause of the error. error_code names the cause; branch on it rather than on message. Absent when the error has no code. Treat an unrecognized error_code as the bare type.

error.details from the response body, if present.



179
180
181
182
183
184
# File 'lib/anthropic/errors.rb', line 179

def details
  Anthropic::Internal::Type::Converter.coerce(
    Anthropic::ErrorDetails,
    Anthropic::Internal::Util.dig(body, [:error, :details])
  )
end

#error_code ⇒ Symbol?

A stable code that names the cause.

error.details.error_code from the response body, if present and a string.

Returns:

  • (Symbol, nil)


167
168
169
170
# File 'lib/anthropic/errors.rb', line 167

def error_code
  code = Anthropic::Internal::Util.dig(body, [:error, :details, :error_code])
  code.to_sym if code.is_a?(String)
end

#type ⇒ Symbol, ...

The broad category of the error.

error.type from the response body, if present.

Returns:



157
158
159
160
# File 'lib/anthropic/errors.rb', line 157

def type
  value = Anthropic::Internal::Util.dig(body, [:error, :type])
  value.is_a?(String) ? value.to_sym : value
end