Claudex.Error exception (Claudex v0.6.1)

Copy Markdown View Source

An error from the Claude API, or from trying to reach it.

:type mirrors the error types the API documents — :billing for 402, :timeout for a 504 as well as for a client-side timeout, and so on. :error_type is the API's own string ("invalid_request_error"), finer grained than the status and extensible by the API, so reach for it when :type isn't specific enough. Match on it to handle specific cases:

case Claudex.Messages.create(client, params) do
  {:ok, message} -> message
  {:error, %Claudex.Error{type: :rate_limit}} -> :retry_later
  {:error, error} -> raise error
end

Summary

Functions

Builds an error from an HTTP response: a status code and its body. The body may be a decoded map or the raw bytes; either way the API's own message and error type end up on the struct.

Builds an error from an error event the API sent part-way through a stream. There's no HTTP status on one of these, so the type comes from the event's own error type.

Builds an error for a request that never got a response (timeout, connection failure).

Builds an error for a stream that didn't hold up its end of the protocol — malformed event data, or a reply that ended before it began.

Types

t()

@type t() :: %Claudex.Error{
  __exception__: true,
  body: term(),
  error_type: String.t() | nil,
  message: String.t(),
  request_id: String.t() | nil,
  status: pos_integer() | nil,
  type: type()
}

type()

@type type() ::
  :bad_request
  | :authentication
  | :permission_denied
  | :not_found
  | :conflict
  | :request_too_large
  | :unprocessable_entity
  | :rate_limit
  | :overloaded
  | :billing
  | :internal_server
  | :api_status
  | :connection
  | :timeout
  | :stream

Functions

from_response(status, body)

@spec from_response(pos_integer(), map() | binary() | nil) :: t()

Builds an error from an HTTP response: a status code and its body. The body may be a decoded map or the raw bytes; either way the API's own message and error type end up on the struct.

from_stream_event(body)

@spec from_stream_event(map()) :: t()

Builds an error from an error event the API sent part-way through a stream. There's no HTTP status on one of these, so the type comes from the event's own error type.

from_transport(exception)

@spec from_transport(Exception.t()) :: t()

Builds an error for a request that never got a response (timeout, connection failure).

stream_error(message, body \\ nil)

@spec stream_error(String.t(), term()) :: t()

Builds an error for a stream that didn't hold up its end of the protocol — malformed event data, or a reply that ended before it began.