Current section
Files
Jump to
Current section
Files
src/glopenai@error.erl
-module(glopenai@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glopenai/error.gleam").
-export([api_error_decoder/0, wrapped_error_decoder/0, api_error_to_json/1]).
-export_type([api_error/0, glopenai_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.
-type api_error() :: {api_error,
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary())}.
-type glopenai_error() :: {api_response_error, integer(), api_error()} |
{json_decode_error, binary(), gleam@json:decode_error()} |
{unexpected_response, integer(), binary()}.
-file("src/glopenai/error.gleam", 27).
?DOC(
" Decode an ApiError from JSON.\n"
" The OpenAI API wraps errors as `{\"error\": {...}}`.\n"
).
-spec api_error_decoder() -> gleam@dynamic@decode:decoder(api_error()).
api_error_decoder() ->
gleam@dynamic@decode:field(
<<"message"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Message) ->
gleam@dynamic@decode:field(
<<"type"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Error_type) ->
gleam@dynamic@decode:field(
<<"param"/utf8>>,
gleam@dynamic@decode:optional(
{decoder, fun gleam@dynamic@decode:decode_string/1}
),
fun(Param) ->
gleam@dynamic@decode:field(
<<"code"/utf8>>,
gleam@dynamic@decode:optional(
{decoder,
fun gleam@dynamic@decode:decode_string/1}
),
fun(Code) ->
gleam@dynamic@decode:success(
{api_error,
Message,
Error_type,
Param,
Code}
)
end
)
end
)
end
)
end
).
-file("src/glopenai/error.gleam", 41).
?DOC(" Decode the wrapped error envelope `{\"error\": ApiError}`.\n").
-spec wrapped_error_decoder() -> gleam@dynamic@decode:decoder(api_error()).
wrapped_error_decoder() ->
gleam@dynamic@decode:field(
<<"error"/utf8>>,
api_error_decoder(),
fun(Error) -> gleam@dynamic@decode:success(Error) end
).
-file("src/glopenai/error.gleam", 47).
?DOC(" Encode an ApiError to JSON.\n").
-spec api_error_to_json(api_error()) -> gleam@json:json().
api_error_to_json(Error) ->
gleam@json:object(
[{<<"message"/utf8>>, gleam@json:string(erlang:element(2, Error))},
{<<"type"/utf8>>,
gleam@json:nullable(
erlang:element(3, Error),
fun gleam@json:string/1
)},
{<<"param"/utf8>>,
gleam@json:nullable(
erlang:element(4, Error),
fun gleam@json:string/1
)},
{<<"code"/utf8>>,
gleam@json:nullable(
erlang:element(5, Error),
fun gleam@json:string/1
)}]
).