Packages

Error handling library for gleam language

Current section

Files

Jump to
geny src ext.erl
Raw

src/ext.erl

-module(ext).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([wrap_error/1, from/1, context/2, lazy_context/2, ensure/3, to_string/1, debug/1]).
-spec append(geny:geny(FSU), list(binary())) -> geny:geny(FSU).
append(Geny, New_contexts) ->
{geny, Value, Contexts} = Geny,
Contexts@1 = gleam@list:concat([Contexts, New_contexts]),
{geny, Value, Contexts@1}.
-spec wrap_error(FSY) -> geny:geny(FSY).
wrap_error(Error) ->
{geny, Error, gleam@list:new()}.
-spec from({ok, FTA} | {error, FTB}) -> {ok, FTA} | {error, geny:geny(FTB)}.
from(Result) ->
case Result of
{ok, A} ->
{ok, A};
{error, E} ->
{error, wrap_error(E)}
end.
-spec context({ok, FTG} | {error, geny:geny(FTH)}, binary()) -> {ok, FTG} |
{error, geny:geny(FTH)}.
context(Result, Context) ->
case Result of
{ok, _} ->
Result;
{error, Geny} ->
{error, append(Geny, [Context])}
end.
-spec lazy_context({ok, FTM} | {error, geny:geny(FTN)}, fun(() -> binary())) -> {ok,
FTM} |
{error, geny:geny(FTN)}.
lazy_context(Result, Context) ->
case Result of
{ok, _} ->
Result;
{error, Geny} ->
{error, append(Geny, [Context()])}
end.
-spec ensure(boolean(), FTU, fun(() -> {ok, FTT} | {error, geny:geny(FTU)})) -> {ok,
FTT} |
{error, geny:geny(FTU)}.
ensure(Must, Error, Otherwise) ->
case Must of
false ->
{error, wrap_error(Error)};
_ ->
Otherwise()
end.
-spec to_string(geny:geny(any())) -> binary().
to_string(Geny) ->
{geny, Error, Contexts} = Geny,
Error@1 = begin
_pipe = Error,
gleam@string:inspect(_pipe)
end,
Error@2 = <<"\nerror: "/utf8, Error@1/binary>>,
Contexts@1 = (gleam@list:index_fold(
Contexts,
<<""/utf8>>,
fun(Acc, Context, Index) ->
<<<<<<<<<<Acc/binary, " "/utf8>>/binary,
(begin
_pipe@1 = Index,
gleam@int:to_string(_pipe@1)
end)/binary>>/binary,
" : "/utf8>>/binary,
Context/binary>>/binary,
"\n"/utf8>>
end
)),
{Line_separator, Contexts@2} = case gleam@string:is_empty(Contexts@1) of
true ->
{<<"\n"/utf8>>, <<""/utf8>>};
false ->
{<<"\n\n"/utf8>>, <<"caused by:\n"/utf8, Contexts@1/binary>>}
end,
<<<<Error@2/binary, Line_separator/binary>>/binary, Contexts@2/binary>>.
-spec debug({ok, FUB} | {error, geny:geny(FUC)}) -> {ok, FUB} |
{error, geny:geny(FUC)}.
debug(Result) ->
gleam@io:println(<<"======= Geny/Debug start......========"/utf8>>),
case Result of
{error, Geny} ->
_pipe = to_string(Geny),
gleam@io:println(_pipe),
nil;
{ok, A} ->
_pipe@1 = A,
gleam@io:debug(_pipe@1),
nil
end,
gleam@io:println(<<"======= Geny/Debug end...... ========"/utf8>>),
gleam@io:println(<<""/utf8>>),
gleam@io:println(<<""/utf8>>),
gleam@io:println(<<""/utf8>>),
Result.