-module(outcome@problem). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([new_defect/1, new_failure/1, map_error/2, tap_error/2, tap_defect/2, tap_failure/2, add_context/2, get_failure/2, extract_error/1, stack_to_lines/1]). -export_type([severity/0, problem/1]). -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 severity() :: defect | failure. -type problem(FWT) :: {problem, FWT, severity(), list(binary())}. -file("src/outcome/problem.gleam", 21). -spec new_problem(FWV, severity()) -> problem(FWV). new_problem(Error, Severity) -> {problem, Error, Severity, []}. -file("src/outcome/problem.gleam", 35). ?DOC( " Create a Defect\n" " Use this if you need the `Problem` type only.\n" " Usually you will use `as_defect` instead.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " problem.new_defect(\"Something went wrong\")\n" " ```\n" ). -spec new_defect(FWX) -> problem(FWX). new_defect(Error) -> new_problem(Error, defect). -file("src/outcome/problem.gleam", 49). ?DOC( " Create a Failure\n" " Use this if you need the `Problem` type only.\n" " Usually you will use `result_to_failure` instead.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " problem.new_failure(\"Something went wrong\")\n" " ```\n" ). -spec new_failure(FWZ) -> problem(FWZ). new_failure(Error) -> new_problem(Error, failure). -file("src/outcome/problem.gleam", 54). ?DOC(false). -spec map_error(problem(FXB), fun((FXB) -> FXB)) -> problem(FXB). map_error(Problem, Mapper) -> _record = Problem, {problem, Mapper(erlang:element(2, Problem)), erlang:element(3, _record), erlang:element(4, _record)}. -file("src/outcome/problem.gleam", 59). ?DOC(false). -spec tap_error(problem(FXE), fun((FXE) -> any())) -> problem(FXE). tap_error(Problem, Fun) -> Fun(erlang:element(2, Problem)), Problem. -file("src/outcome/problem.gleam", 65). ?DOC(false). -spec tap_defect(problem(FXI), fun((FXI) -> any())) -> problem(FXI). tap_defect(Problem, Fun) -> case erlang:element(3, Problem) of defect -> Fun(erlang:element(2, Problem)), Problem; _ -> Problem end. -file("src/outcome/problem.gleam", 76). ?DOC(false). -spec tap_failure(problem(FXM), fun((FXM) -> any())) -> problem(FXM). tap_failure(Problem, Fun) -> case erlang:element(3, Problem) of failure -> Fun(erlang:element(2, Problem)), Problem; _ -> Problem end. -file("src/outcome/problem.gleam", 86). -spec push_to_stack(list(binary()), binary()) -> list(binary()). push_to_stack(Stack, Entry) -> [Entry | Stack]. -file("src/outcome/problem.gleam", 91). ?DOC(false). -spec add_context(problem(FXR), binary()) -> problem(FXR). add_context(Problem, Value) -> _record = Problem, {problem, erlang:element(2, _record), erlang:element(3, _record), push_to_stack(erlang:element(4, Problem), Value)}. -file("src/outcome/problem.gleam", 107). ?DOC( " Use this to show a failure to a user.\n" " Extracts the Error value from a `Problem` when the severity is `Failure`.\n" " otherwise it will return the default value given.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " case result {\n" " Ok(value) -> io.debug(\"Success\")\n" " Error(problem) -> io.error(problem.get_failure(problem, \"Something went wrong\"))\n" " }\n" " ```\n" ). -spec get_failure(problem(FXU), FXU) -> FXU. get_failure(Problem, Default_value) -> case erlang:element(3, Problem) of defect -> Default_value; failure -> erlang:element(2, Problem) end. -file("src/outcome/problem.gleam", 115). ?DOC(false). -spec extract_error(problem(FXW)) -> FXW. extract_error(Problem) -> erlang:element(2, Problem). -file("src/outcome/problem.gleam", 120). ?DOC(false). -spec stack_to_lines(list(binary())) -> list(binary()). stack_to_lines(Stack) -> _pipe = Stack, lists:reverse(_pipe).