Current section
Files
Jump to
Current section
Files
src/telega@error.erl
-module(telega@error).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/telega/error.gleam").
-export([to_string/1, is_message_not_modified/1, is_message_not_found/1, is_message_cant_be_edited/1, 'try'/3]).
-export_type([telega_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 telega_error() :: {telegram_api_error, integer(), binary()} |
{fetch_error, binary()} |
{json_decode_error, gleam@json:decode_error()} |
{bot_handle_update_error, binary()} |
api_to_request_convert_error |
set_webhook_error |
no_session_settings_error |
{registry_start_error, binary()} |
{bot_start_error, gleam@otp@actor:start_error()} |
{chat_instance_start_error, gleam@otp@actor:start_error()} |
file_not_found_error |
{decode_update_error, binary()} |
{unknown_update_error, telega@model@types:update()} |
{actor_error, binary()} |
{router_error, binary()} |
{supervisor_start_error, gleam@otp@actor:start_error()} |
{shutdown_error, binary()}.
-file("src/telega/error.gleam", 46).
-spec to_string(telega_error()) -> binary().
to_string(Error) ->
case Error of
{telegram_api_error, Error_code, Description} ->
<<<<<<"Telegram API error: "/utf8,
(erlang:integer_to_binary(Error_code))/binary>>/binary,
" "/utf8>>/binary,
Description/binary>>;
{json_decode_error, Error@1} ->
<<"Decode JSON error: "/utf8,
(gleam@string:inspect(Error@1))/binary>>;
api_to_request_convert_error ->
<<"Failed to convert API request to HTTP request"/utf8>>;
{fetch_error, Error@2} ->
<<"Failed to send request: "/utf8,
(gleam@string:inspect(Error@2))/binary>>;
set_webhook_error ->
<<"Failed to set webhook"/utf8>>;
no_session_settings_error ->
<<"Session settings not initialized"/utf8>>;
{registry_start_error, Reason} ->
<<"Failed to start registry: "/utf8, Reason/binary>>;
{bot_start_error, Reason@1} ->
<<"Failed to start bot: "/utf8,
(gleam@string:inspect(Reason@1))/binary>>;
{chat_instance_start_error, Reason@2} ->
<<"Failed to start chat instance: "/utf8,
(gleam@string:inspect(Reason@2))/binary>>;
file_not_found_error ->
<<"File not found"/utf8>>;
{decode_update_error, Reason@3} ->
<<"Failed to decode update: "/utf8, Reason@3/binary>>;
{bot_handle_update_error, Reason@4} ->
<<"Failed to handle update: "/utf8, Reason@4/binary>>;
{unknown_update_error, Update} ->
<<"Unknown update: "/utf8, (gleam@string:inspect(Update))/binary>>;
{actor_error, Reason@5} ->
<<"Actor error: "/utf8, Reason@5/binary>>;
{router_error, Reason@6} ->
<<"Router error: "/utf8, Reason@6/binary>>;
{supervisor_start_error, Reason@7} ->
<<"Failed to start supervisor: "/utf8,
(gleam@string:inspect(Reason@7))/binary>>;
{shutdown_error, Reason@8} ->
<<"Shutdown error: "/utf8, Reason@8/binary>>
end.
-file("src/telega/error.gleam", 96).
-spec is_400_with(telega_error(), binary()) -> boolean().
is_400_with(Error, Needle) ->
case Error of
{telegram_api_error, 400, Description} ->
gleam_stdlib:contains_string(string:lowercase(Description), Needle);
_ ->
false
end.
-file("src/telega/error.gleam", 76).
?DOC(
" The edit was a no-op: new content equals the current one. Safe to treat\n"
" as success.\n"
"\n"
" The Bot API has no structured error codes, so a case-insensitive substring\n"
" match on the 400 description is the only way to classify this error.\n"
).
-spec is_message_not_modified(telega_error()) -> boolean().
is_message_not_modified(Error) ->
is_400_with(Error, <<"message is not modified"/utf8>>).
-file("src/telega/error.gleam", 84).
?DOC(
" The message to edit no longer exists (deleted by the user or too old).\n"
"\n"
" The Bot API has no structured error codes, so a case-insensitive substring\n"
" match on the 400 description is the only way to classify this error.\n"
).
-spec is_message_not_found(telega_error()) -> boolean().
is_message_not_found(Error) ->
is_400_with(Error, <<"message to edit not found"/utf8>>).
-file("src/telega/error.gleam", 92).
?DOC(
" The message exists but cannot be edited (e.g. not sent by the bot).\n"
"\n"
" The Bot API has no structured error codes, so a case-insensitive substring\n"
" match on the 400 description is the only way to classify this error.\n"
).
-spec is_message_cant_be_edited(telega_error()) -> boolean().
is_message_cant_be_edited(Error) ->
is_400_with(Error, <<"message can't be edited"/utf8>>).
-file("src/telega/error.gleam", 105).
?DOC(" Helper to replace `result.try` for api call and error mapping.\n").
-spec 'try'(
{ok, JYK} | {error, telega_error()},
fun((telega_error()) -> JYN),
fun((JYK) -> {ok, JYO} | {error, JYN})
) -> {ok, JYO} | {error, JYN}.
'try'(Result, To_error, Fun) ->
case Result of
{ok, X} ->
Fun(X);
{error, E} ->
{error, To_error(E)}
end.