-module(telega@error). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([to_string/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, gleam@dynamic:dynamic_()} | {json_decode_error, gleam@json:decode_error()} | api_to_request_convert_error | set_webhook_error | no_session_settings_error | {registry_start_error, binary()} | {bot_start_error, binary()} | {chat_instance_start_error, binary()} | file_not_found_error | {decode_update_error, binary()} | {unknown_update_error, telega@model:update()}. -file("src/telega/error.gleam", 30). -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, Reason@1/binary>>; {chat_instance_start_error, Reason@2} -> <<"Failed to start chat instance: "/utf8, Reason@2/binary>>; file_not_found_error -> <<"File not found"/utf8>>; {decode_update_error, Reason@3} -> <<"Failed to decode update: "/utf8, Reason@3/binary>>; {unknown_update_error, Update} -> <<"Unknown update: "/utf8, (gleam@string:inspect(Update))/binary>> end. -file("src/telega/error.gleam", 50). ?DOC(" Helper to replace `result.try` for api call and error mapping.\n"). -spec 'try'( {ok, AMUU} | {error, telega_error()}, fun((telega_error()) -> AMUX), fun((AMUU) -> {ok, AMUY} | {error, AMUX}) ) -> {ok, AMUY} | {error, AMUX}. 'try'(Result, To_error, Fun) -> case Result of {ok, X} -> Fun(X); {error, E} -> {error, To_error(E)} end.