-module(anthropic@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/anthropic/error.gleam").
-export([api_error_type_from_string/1, api_error_type_to_string/1, api_error_details/2, api_error_details_full/4, api_error_details_to_string/1, api_error/2, authentication_error/1, invalid_request_error/1, rate_limit_error/1, internal_api_error/1, overloaded_error/1, http_error/1, json_error/1, config_error/1, timeout_error/1, network_error/1, missing_api_key_error/0, invalid_api_key_error/0, error_to_string/1, error_category/1, is_retryable/1, is_authentication_error/1, is_rate_limit_error/1, is_overloaded_error/1, get_status_code/1, api_error_details_to_json/1, error_to_json/1, error_to_json_string/1]).
-export_type([api_error_type/0, api_error_details/0, anthropic_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Error types for Anthropic API interactions\n"
"\n"
" This module defines comprehensive error types that can occur when\n"
" interacting with the Anthropic Messages API.\n"
).
-type api_error_type() :: authentication_error |
invalid_request_error |
rate_limit_error |
internal_api_error |
overloaded_error |
permission_error |
not_found_error |
{unknown_api_error, binary()}.
-type api_error_details() :: {api_error_details,
api_error_type(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type anthropic_error() :: {api_error, integer(), api_error_details()} |
{http_error, binary()} |
{json_error, binary()} |
{config_error, binary()} |
{timeout_error, integer()} |
{network_error, binary()}.
-file("src/anthropic/error.gleam", 35).
?DOC(" Convert an API error type string to ApiErrorType\n").
-spec api_error_type_from_string(binary()) -> api_error_type().
api_error_type_from_string(Str) ->
case Str of
<<"authentication_error"/utf8>> ->
authentication_error;
<<"invalid_request_error"/utf8>> ->
invalid_request_error;
<<"rate_limit_error"/utf8>> ->
rate_limit_error;
<<"api_error"/utf8>> ->
internal_api_error;
<<"overloaded_error"/utf8>> ->
overloaded_error;
<<"permission_error"/utf8>> ->
permission_error;
<<"not_found_error"/utf8>> ->
not_found_error;
_ ->
{unknown_api_error, Str}
end.
-file("src/anthropic/error.gleam", 49).
?DOC(" Convert an ApiErrorType to its string representation\n").
-spec api_error_type_to_string(api_error_type()) -> binary().
api_error_type_to_string(Error_type) ->
case Error_type of
authentication_error ->
<<"authentication_error"/utf8>>;
invalid_request_error ->
<<"invalid_request_error"/utf8>>;
rate_limit_error ->
<<"rate_limit_error"/utf8>>;
internal_api_error ->
<<"api_error"/utf8>>;
overloaded_error ->
<<"overloaded_error"/utf8>>;
permission_error ->
<<"permission_error"/utf8>>;
not_found_error ->
<<"not_found_error"/utf8>>;
{unknown_api_error, S} ->
S
end.
-file("src/anthropic/error.gleam", 81).
?DOC(" Create ApiErrorDetails with just type and message\n").
-spec api_error_details(api_error_type(), binary()) -> api_error_details().
api_error_details(Error_type, Message) ->
{api_error_details, Error_type, Message, none, none}.
-file("src/anthropic/error.gleam", 94).
?DOC(" Create ApiErrorDetails with all fields\n").
-spec api_error_details_full(
api_error_type(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary())
) -> api_error_details().
api_error_details_full(Error_type, Message, Param, Code) ->
{api_error_details, Error_type, Message, Param, Code}.
-file("src/anthropic/error.gleam", 109).
?DOC(" Convert ApiErrorDetails to a display string\n").
-spec api_error_details_to_string(api_error_details()) -> binary().
api_error_details_to_string(Details) ->
Base = <<<<(api_error_type_to_string(erlang:element(2, Details)))/binary,
": "/utf8>>/binary,
(erlang:element(3, Details))/binary>>,
With_param = case erlang:element(4, Details) of
{some, P} ->
<<<<<>/binary, P/binary>>/binary,
")"/utf8>>;
none ->
Base
end,
case erlang:element(5, Details) of
{some, C} ->
<<<<<>/binary, C/binary>>/binary,
"]"/utf8>>;
none ->
With_param
end.
-file("src/anthropic/error.gleam", 174).
?DOC(" Create an API error with status code and details\n").
-spec api_error(integer(), api_error_details()) -> anthropic_error().
api_error(Status_code, Details) ->
{api_error, Status_code, Details}.
-file("src/anthropic/error.gleam", 179).
?DOC(" Create an authentication error\n").
-spec authentication_error(binary()) -> anthropic_error().
authentication_error(Message) ->
{api_error, 401, api_error_details(authentication_error, Message)}.
-file("src/anthropic/error.gleam", 187).
?DOC(" Create an invalid request error\n").
-spec invalid_request_error(binary()) -> anthropic_error().
invalid_request_error(Message) ->
{api_error, 400, api_error_details(invalid_request_error, Message)}.
-file("src/anthropic/error.gleam", 195).
?DOC(" Create a rate limit error\n").
-spec rate_limit_error(binary()) -> anthropic_error().
rate_limit_error(Message) ->
{api_error, 429, api_error_details(rate_limit_error, Message)}.
-file("src/anthropic/error.gleam", 203).
?DOC(" Create an internal API error\n").
-spec internal_api_error(binary()) -> anthropic_error().
internal_api_error(Message) ->
{api_error, 500, api_error_details(internal_api_error, Message)}.
-file("src/anthropic/error.gleam", 211).
?DOC(" Create an overloaded error\n").
-spec overloaded_error(binary()) -> anthropic_error().
overloaded_error(Message) ->
{api_error, 529, api_error_details(overloaded_error, Message)}.
-file("src/anthropic/error.gleam", 219).
?DOC(" Create an HTTP error\n").
-spec http_error(binary()) -> anthropic_error().
http_error(Reason) ->
{http_error, Reason}.
-file("src/anthropic/error.gleam", 224).
?DOC(" Create a JSON error\n").
-spec json_error(binary()) -> anthropic_error().
json_error(Reason) ->
{json_error, Reason}.
-file("src/anthropic/error.gleam", 229).
?DOC(" Create a configuration error\n").
-spec config_error(binary()) -> anthropic_error().
config_error(Reason) ->
{config_error, Reason}.
-file("src/anthropic/error.gleam", 234).
?DOC(" Create a timeout error\n").
-spec timeout_error(integer()) -> anthropic_error().
timeout_error(Timeout_ms) ->
{timeout_error, Timeout_ms}.
-file("src/anthropic/error.gleam", 239).
?DOC(" Create a network error\n").
-spec network_error(binary()) -> anthropic_error().
network_error(Reason) ->
{network_error, Reason}.
-file("src/anthropic/error.gleam", 244).
?DOC(" Create a missing API key error\n").
-spec missing_api_key_error() -> anthropic_error().
missing_api_key_error() ->
{config_error, <<"API key is required but not provided"/utf8>>}.
-file("src/anthropic/error.gleam", 249).
?DOC(" Create an invalid API key error\n").
-spec invalid_api_key_error() -> anthropic_error().
invalid_api_key_error() ->
{config_error, <<"API key format is invalid"/utf8>>}.
-file("src/anthropic/error.gleam", 258).
?DOC(" Convert an AnthropicError to a human-readable string\n").
-spec error_to_string(anthropic_error()) -> binary().
error_to_string(Error) ->
case Error of
{api_error, Status, Details} ->
<<<<<<"API Error ("/utf8,
(erlang:integer_to_binary(Status))/binary>>/binary,
"): "/utf8>>/binary,
(api_error_details_to_string(Details))/binary>>;
{http_error, Reason} ->
<<"HTTP Error: "/utf8, Reason/binary>>;
{json_error, Reason@1} ->
<<"JSON Error: "/utf8, Reason@1/binary>>;
{config_error, Reason@2} ->
<<"Configuration Error: "/utf8, Reason@2/binary>>;
{timeout_error, Ms} ->
<<<<"Timeout Error: Request timed out after "/utf8,
(erlang:integer_to_binary(Ms))/binary>>/binary,
"ms"/utf8>>;
{network_error, Reason@3} ->
<<"Network Error: "/utf8, Reason@3/binary>>
end.
-file("src/anthropic/error.gleam", 280).
?DOC(" Get the error category as a string\n").
-spec error_category(anthropic_error()) -> binary().
error_category(Error) ->
case Error of
{api_error, _, _} ->
<<"api"/utf8>>;
{http_error, _} ->
<<"http"/utf8>>;
{json_error, _} ->
<<"json"/utf8>>;
{config_error, _} ->
<<"config"/utf8>>;
{timeout_error, _} ->
<<"timeout"/utf8>>;
{network_error, _} ->
<<"network"/utf8>>
end.
-file("src/anthropic/error.gleam", 296).
?DOC(" Check if an error is retryable\n").
-spec is_retryable(anthropic_error()) -> boolean().
is_retryable(Error) ->
case Error of
{api_error, Status, Details} ->
case erlang:element(2, Details) of
rate_limit_error ->
true;
overloaded_error ->
true;
internal_api_error ->
Status >= 500;
_ ->
false
end;
{http_error, _} ->
true;
{timeout_error, _} ->
true;
{network_error, _} ->
true;
{json_error, _} ->
false;
{config_error, _} ->
false
end.
-file("src/anthropic/error.gleam", 314).
?DOC(" Check if an error is an authentication error\n").
-spec is_authentication_error(anthropic_error()) -> boolean().
is_authentication_error(Error) ->
case Error of
{api_error, _, Details} ->
case erlang:element(2, Details) of
authentication_error ->
true;
_ ->
false
end;
_ ->
false
end.
-file("src/anthropic/error.gleam", 326).
?DOC(" Check if an error is a rate limit error\n").
-spec is_rate_limit_error(anthropic_error()) -> boolean().
is_rate_limit_error(Error) ->
case Error of
{api_error, _, Details} ->
case erlang:element(2, Details) of
rate_limit_error ->
true;
_ ->
false
end;
_ ->
false
end.
-file("src/anthropic/error.gleam", 338).
?DOC(" Check if an error is an overloaded error\n").
-spec is_overloaded_error(anthropic_error()) -> boolean().
is_overloaded_error(Error) ->
case Error of
{api_error, _, Details} ->
case erlang:element(2, Details) of
overloaded_error ->
true;
_ ->
false
end;
_ ->
false
end.
-file("src/anthropic/error.gleam", 350).
?DOC(" Get the HTTP status code if this is an API error\n").
-spec get_status_code(anthropic_error()) -> gleam@option:option(integer()).
get_status_code(Error) ->
case Error of
{api_error, Status, _} ->
{some, Status};
_ ->
none
end.
-file("src/anthropic/error.gleam", 362).
?DOC(" Encode an ApiErrorDetails to JSON\n").
-spec api_error_details_to_json(api_error_details()) -> gleam@json:json().
api_error_details_to_json(Details) ->
Base_fields = [{<<"type"/utf8>>,
gleam@json:string(
api_error_type_to_string(erlang:element(2, Details))
)},
{<<"message"/utf8>>, gleam@json:string(erlang:element(3, Details))}],
With_param = case erlang:element(4, Details) of
{some, P} ->
[{<<"param"/utf8>>, gleam@json:string(P)} | Base_fields];
none ->
Base_fields
end,
With_code = case erlang:element(5, Details) of
{some, C} ->
[{<<"code"/utf8>>, gleam@json:string(C)} | With_param];
none ->
With_param
end,
gleam@json:object(With_code).
-file("src/anthropic/error.gleam", 382).
?DOC(" Encode an AnthropicError to JSON\n").
-spec error_to_json(anthropic_error()) -> gleam@json:json().
error_to_json(Error) ->
case Error of
{api_error, Status, Details} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"api"/utf8>>)},
{<<"status_code"/utf8>>, gleam@json:int(Status)},
{<<"error"/utf8>>, api_error_details_to_json(Details)}]
);
{http_error, Reason} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"http"/utf8>>)},
{<<"reason"/utf8>>, gleam@json:string(Reason)}]
);
{json_error, Reason@1} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"json"/utf8>>)},
{<<"reason"/utf8>>, gleam@json:string(Reason@1)}]
);
{config_error, Reason@2} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"config"/utf8>>)},
{<<"reason"/utf8>>, gleam@json:string(Reason@2)}]
);
{timeout_error, Ms} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"timeout"/utf8>>)},
{<<"timeout_ms"/utf8>>, gleam@json:int(Ms)}]
);
{network_error, Reason@3} ->
gleam@json:object(
[{<<"category"/utf8>>, gleam@json:string(<<"network"/utf8>>)},
{<<"reason"/utf8>>, gleam@json:string(Reason@3)}]
)
end.
-file("src/anthropic/error.gleam", 424).
?DOC(" Convert an error to a JSON string\n").
-spec error_to_json_string(anthropic_error()) -> binary().
error_to_json_string(Error) ->
_pipe = Error,
_pipe@1 = error_to_json(_pipe),
gleam@json:to_string(_pipe@1).