Current section

Files

Jump to
outcome src outcome.erl
Raw

src/outcome.erl

-module(outcome).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([as_defect/1, as_failure/1, map_error/2, tap/2, tap_error/2, tap_defect/2, tap_failure/2, context/2, to_simple_result/1, pretty_print/2, print_line/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/outcome.gleam", 18).
?DOC(
" Convert `Result(a, e)` to `Result(a, Problem(e))`\n"
" with severity as `Defect`\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"Something went wrong\")\n"
" |> outcome.as_defect\n"
" ```\n"
).
-spec as_defect({ok, FZH} | {error, FZI}) -> {ok, FZH} |
{error, outcome@problem:problem(FZI)}.
as_defect(Result) ->
gleam@result:map_error(Result, fun outcome@problem:new_defect/1).
-file("src/outcome.gleam", 31).
?DOC(
" Convert `Result(a, e)` to `Result(a, Problem(e))`\n"
" with severity as `Failure`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"Invalid input\")\n"
" |> outcome.as_failure\n"
" ```\n"
).
-spec as_failure({ok, FZN} | {error, FZO}) -> {ok, FZN} |
{error, outcome@problem:problem(FZO)}.
as_failure(Result) ->
gleam@result:map_error(Result, fun outcome@problem:new_failure/1).
-file("src/outcome.gleam", 36).
?DOC(" Map the error value\n").
-spec map_error(
{ok, FZT} | {error, outcome@problem:problem(FZU)},
fun((FZU) -> FZU)
) -> {ok, FZT} | {error, outcome@problem:problem(FZU)}.
map_error(Outcome, Mapper) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> outcome@problem:map_error(_capture, Mapper) end
).
-file("src/outcome.gleam", 49).
?DOC(
" Use tap functions to log the errors.\n"
" This yields the `Problem` type.\n"
).
-spec tap(
{ok, FZZ} | {error, outcome@problem:problem(GAA)},
fun((outcome@problem:problem(GAA)) -> any())
) -> {ok, FZZ} | {error, outcome@problem:problem(GAA)}.
tap(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(Problem) ->
Fun(Problem),
Problem
end
).
-file("src/outcome.gleam", 60).
?DOC(" This yields your error type.\n").
-spec tap_error(
{ok, GAH} | {error, outcome@problem:problem(GAI)},
fun((GAI) -> any())
) -> {ok, GAH} | {error, outcome@problem:problem(GAI)}.
tap_error(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> outcome@problem:tap_error(_capture, Fun) end
).
-file("src/outcome.gleam", 69).
?DOC(
" Yield your error type.\n"
" Only called if the severity is Defect.\n"
).
-spec tap_defect(
{ok, GAO} | {error, outcome@problem:problem(GAP)},
fun((GAP) -> any())
) -> {ok, GAO} | {error, outcome@problem:problem(GAP)}.
tap_defect(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> outcome@problem:tap_defect(_capture, Fun) end
).
-file("src/outcome.gleam", 78).
?DOC(
" Yield your error type.\n"
" Only called if the severity is Failure.\n"
).
-spec tap_failure(
{ok, GAV} | {error, outcome@problem:problem(GAW)},
fun((GAW) -> any())
) -> {ok, GAV} | {error, outcome@problem:problem(GAW)}.
tap_failure(Outcome, Fun) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> outcome@problem:tap_failure(_capture, Fun) end
).
-file("src/outcome.gleam", 96).
?DOC(
" Add context to an Outcome.\n"
" This will add a Context entry to the stack.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"Something went wrong\")\n"
" |> outcome.as_defect\n"
" |> outcome.context(\"In find user function\")\n"
" ```\n"
).
-spec context({ok, GBC} | {error, outcome@problem:problem(GBD)}, binary()) -> {ok,
GBC} |
{error, outcome@problem:problem(GBD)}.
context(Outcome, Context) ->
gleam@result:map_error(
Outcome,
fun(_capture) -> outcome@problem:add_context(_capture, Context) end
).
-file("src/outcome.gleam", 112).
?DOC(
" Remove the `Problem` wrapping in the error value\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let result = Error(\"Fail\") |> outcome.as_defect\n"
"\n"
" outcome.to_simple_result(result) == Error(\"Fail\")\n"
" ```\n"
).
-spec to_simple_result({ok, GBI} | {error, outcome@problem:problem(GBJ)}) -> {ok,
GBI} |
{error, GBJ}.
to_simple_result(Outcome) ->
_pipe = Outcome,
gleam@result:map_error(_pipe, fun outcome@problem:extract_error/1).
-file("src/outcome.gleam", 189).
-spec long_suffix(outcome@problem:severity()) -> binary().
long_suffix(Severity) ->
case Severity of
defect ->
<<"Defect: "/utf8>>;
failure ->
<<"Failure: "/utf8>>
end.
-file("src/outcome.gleam", 185).
-spec prettry_print_problem_error(outcome@problem:severity(), binary()) -> binary().
prettry_print_problem_error(Severity, Error) ->
<<(long_suffix(Severity))/binary, Error/binary>>.
-file("src/outcome.gleam", 162).
-spec pretty_print_with_joins(
outcome@problem:problem(GBS),
binary(),
binary(),
fun((GBS) -> binary())
) -> binary().
pretty_print_with_joins(Problem, Join_current, Join_stack, To_s) ->
Current = prettry_print_problem_error(
erlang:element(3, Problem),
To_s(erlang:element(2, Problem))
),
Stack = <<Join_current/binary,
(begin
_pipe = erlang:element(4, Problem),
_pipe@1 = outcome@problem:stack_to_lines(_pipe),
gleam@string:join(_pipe@1, Join_stack)
end)/binary>>,
Stack@1 = case erlang:element(4, Problem) of
[] ->
<<""/utf8>>;
_ ->
Stack
end,
<<Current/binary, Stack@1/binary>>.
-file("src/outcome.gleam", 140).
?DOC(
" Pretty print a Problem, including the stack.\n"
" The latest problem appears at the top of the stack.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"Something went wrong\")\n"
" |> outcome.as_defect\n"
" |> outcome.context(\"In find user function\")\n"
" |> outcome.context(\"More context\")\n"
" |> outcome.pretty_print(function.identity)\n"
" ```\n"
"\n"
" ```\n"
" Defect: Something went wrong\n"
"\n"
" stack:\n"
" In find user function\n"
" More context\n"
" ```\n"
).
-spec pretty_print(outcome@problem:problem(GBO), fun((GBO) -> binary())) -> binary().
pretty_print(Problem, To_s) ->
pretty_print_with_joins(
Problem,
<<"\n\nstack:\n "/utf8>>,
<<"\n "/utf8>>,
To_s
).
-file("src/outcome.gleam", 158).
?DOC(
" Print problem in one line\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" Error(\"Something went wrong\")\n"
" |> outcome.as_defect\n"
" |> outcome.context(\"In find user function\")\n"
" |> outcome.print_line(function.identity)\n"
" ```\n"
"\n"
" ```\n"
" Defect: Something went wrong < In find user function\n"
" ```\n"
).
-spec print_line(outcome@problem:problem(GBQ), fun((GBQ) -> binary())) -> binary().
print_line(Problem, To_s) ->
pretty_print_with_joins(Problem, <<" < "/utf8>>, <<" < "/utf8>>, To_s).