OkThen.Result.error_map
You're seeing just the function
error_map
, go back to OkThen.Result module for more information.
Link to this function
error_map(result, func_or_value)
Specs
error_map(t, func_or_value(out)) :: t | :error | error(out) when t: result_input(), out: any()
If result
is tagged :error
, transforms the wrapped value by passing it into the provided
mapping function, and replacing it with the returned value. If func_or_value
is not a
function, then it is used directly as the new value.
If the new value would be nil
, then :none
is returned as the result instead. Consider piping
into |> none_then({:error, nil})
if you really want {:error, nil}
. See none_then/2
.
If result
is not tagged :error
, result
is returned as-is.
Equivalent to tagged_map(result, :error, func_or_value)
. See tagged_map/3
.
Examples
iex> :error |> Result.error_map("hello")
{:error, "hello"}
iex> {:error, 1} |> Result.error_map("hello")
{:error, "hello"}
iex> {:error, 1} |> Result.error_map(nil)
:none
iex> :none |> Result.error_map("hello")
:none
iex> :error |> Result.error_map(fn {} -> "hello" end)
{:error, "hello"}
iex> {:error, 1} |> Result.error_map(fn 1 -> "hello" end)
{:error, "hello"}
iex> {:error, 1, 2} |> Result.error_map(fn {1, 2} -> "hello" end)
{:error, "hello"}
iex> {:error, 1, 2} |> Result.error_map(fn 1, 2 -> "hello" end)
** (ArgumentError) Value-mapping function must have arity between 0 and 1.
iex> {:error, 1, 2} |> Result.error_map(fn {1, 2} -> {} end)
:error
iex> :ok |> Result.error_map(fn _ -> "hello" end)
:ok
iex> {:ok, 1} |> Result.error_map(fn _ -> "hello" end)
{:ok, 1}
iex> {:ok, 1, 2} |> Result.error_map(fn _ -> "hello" end)
{:ok, 1, 2}
iex> :none |> Result.error_map(fn _ -> "hello" end)
:none
iex> :something_else |> Result.error_map(fn _ -> "hello" end)
:something_else
iex> "bare value" |> Result.error_map(fn _ -> "hello" end)
"bare value"
iex> "bare value" |> Result.error_map("hello")
"bare value"