StrawHat v0.5.0 StrawHat.Response View Source
Utilities for working with "result tuples".
{:ok, value}
{:error, reason}
Link to this section Summary
Functions
Calls the next function only if it receives an ok tuple. Otherwise it skips the call and returns the error tuple
Calls the first function if it receives an error tuple, and the second one if it receives an ok tuple
Creates a new error result tuple
Checks if a result_tuple
is an error
Promotes any value to a result tuple
Converts an Ok
value to an Error
value if the predicate
is not valid
Calls the next function only if it receives an ok tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an ok tuple
Calls the next function only if it receives an error tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an error tuple
Creates a new ok result tuple
Checks if a result_tuple
is ok
Calls the next function only if it receives an error tuple. Otherwise it skips the call and returns the ok tuple. It expects the function to return a new result tuple
Converts an Ok
value to an Error
value if the predicate
is valid
Transforms a list of result tuple to a result tuple containing either the first error tuple or an ok tuple containing the list of values
Calls the next function only if it receives an ok tuple but discards the result. It always returns the original tuple
Calls the next function only if it receives an error tuple but discards the result. It always returns the original tuple
Returns the content of an ok tuple if the value is correct. Otherwise it returns the default value
Link to this section Types
error_tuple()
View Source
error_tuple() :: {:error, any()}
error_tuple() :: {:error, any()}
ok_tuple()
View Source
ok_tuple() :: {:ok, any()}
ok_tuple() :: {:ok, any()}
result_tuple()
View Source
result_tuple() :: ok_tuple() | error_tuple()
result_tuple() :: ok_tuple() | error_tuple()
t(ok, error)
View Source
t(ok, error) :: {:ok, ok} | {:error, error}
t(ok, error) :: {:ok, ok} | {:error, error}
Link to this section Functions
and_then(error, function)
View Source
and_then(result_tuple(), (any() -> result_tuple())) :: result_tuple()
and_then(result_tuple(), (any() -> result_tuple())) :: result_tuple()
Calls the next function only if it receives an ok tuple. Otherwise it skips the call and returns the error tuple.
Examples
iex> business_logic = fn x -> StrawHat.Response.ok(x * 2) end
...> 21 |> StrawHat.Response.ok() |> StrawHat.Response.and_then(business_logic)
{:ok, 42}
iex> business_logic = fn x -> StrawHat.Response.ok(x * 2) end
...> "oops" |> StrawHat.Response.error() |> StrawHat.Response.and_then(business_logic)
{:error, "oops"}
either(arg, on_error, on_ok) View Source
Calls the first function if it receives an error tuple, and the second one if it receives an ok tuple.
Examples
iex> on_ok = fn x -> "X is #{x}" end
...> on_error = fn e -> "Error: #{e}" end
...> 42 |> StrawHat.Response.ok() |> StrawHat.Response.either(on_error, on_ok)
"X is 42"
iex> on_ok = fn x -> "X is #{x}" end
...> on_error = fn e -> "Error: #{e}" end
...> "oops" |> StrawHat.Response.error() |> StrawHat.Response.either(on_error, on_ok)
"Error: oops"
error(value)
View Source
error(any()) :: error_tuple()
error(any()) :: error_tuple()
Creates a new error result tuple.
Examples
iex> StrawHat.Response.error("oops")
{:error, "oops"}
error?(arg)
View Source
error?(result_tuple()) :: boolean()
error?(result_tuple()) :: boolean()
Checks if a result_tuple
is an error.
Examples
iex> 1 |> StrawHat.Response.ok() |> StrawHat.Response.error?()
false
iex> 2 |>StrawHat.Response.error() |> StrawHat.Response.error?()
true
from_value(value, on_nil_value \\ :no_value) View Source
Promotes any value to a result tuple.
It excludes nil
for the ok tuples.
Examples
iex> StrawHat.Response.from_value(nil)
{:error, :no_value}
iex> StrawHat.Response.from_value(nil, "Missing")
{:error, "Missing"}
iex> StrawHat.Response.from_value(42)
{:ok, 42}
iex> StrawHat.Response.from_value({:ok, 123})
{:ok, 123}
iex> StrawHat.Response.from_value({:error, "my error"})
{:error, "my error"}
keep_if(result, predicate, error_message \\ :invalid)
View Source
keep_if(result_tuple(), (any() -> boolean()), any()) :: result_tuple()
keep_if(result_tuple(), (any() -> boolean()), any()) :: result_tuple()
Converts an Ok
value to an Error
value if the predicate
is not valid.
Examples
iex> res = StrawHat.Response.ok(10)
...> StrawHat.Response.keep_if(res, &(&1 > 5))
{:ok, 10}
iex> res = StrawHat.Response.ok(10)
...> StrawHat.Response.keep_if(res, &(&1 > 10), "must be > of 10")
{:error, "must be > of 10"}
iex> res = StrawHat.Response.error(:no_value)
...> StrawHat.Response.keep_if(res, &(&1 > 10), "must be > of 10")
{:error, :no_value}
map(error, function)
View Source
map(result_tuple(), (any() -> any())) :: result_tuple()
map(result_tuple(), (any() -> any())) :: result_tuple()
Calls the next function only if it receives an ok tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an ok tuple.
Examples
iex> business_logic = fn x -> x * 2 end
...> 21 |> StrawHat.Response.ok() |> StrawHat.Response.map(business_logic)
{:ok, 42}
iex> business_logic = fn x -> x * 2 end
...> "oops" |> StrawHat.Response.error() |> StrawHat.Response.map(business_logic)
{:error, "oops"}
map_error(data, function)
View Source
map_error(result_tuple(), (any() -> any())) :: result_tuple()
map_error(result_tuple(), (any() -> any())) :: result_tuple()
Calls the next function only if it receives an error tuple. The function unwraps the value from the tuple, calls the next function and wraps it back into an error tuple.
Examples
iex> better_error = fn _ -> "A better error message" end
...> 42 |> StrawHat.Response.ok() |> StrawHat.Response.map_error(better_error)
{:ok, 42}
iex> better_error = fn _ -> "A better error message" end
...> "oops" |> StrawHat.Response.error() |> StrawHat.Response.map_error(better_error)
{:error, "A better error message"}
ok(value) View Source
Creates a new ok result tuple.
Examples
iex> StrawHat.Response.ok(42)
{:ok, 42}
ok?(arg)
View Source
ok?(result_tuple()) :: boolean()
ok?(result_tuple()) :: boolean()
Checks if a result_tuple
is ok.
Examples
iex> 1 |> StrawHat.Response.ok() |> StrawHat.Response.ok?()
true
iex> 2 |> StrawHat.Response.error() |>StrawHat.Response.ok?()
false
or_else(data, function)
View Source
or_else(result_tuple(), (any() -> result_tuple())) :: result_tuple()
or_else(result_tuple(), (any() -> result_tuple())) :: result_tuple()
Calls the next function only if it receives an error tuple. Otherwise it skips the call and returns the ok tuple. It expects the function to return a new result tuple.
Examples
iex> business_logic = fn _ -> {:error, "a better error message"} end
...> {:ok, 42} |> StrawHat.Response.or_else(business_logic)
{:ok, 42}
iex> business_logic = fn _ -> {:error, "a better error message"} end
...> {:error, "oops"} |> StrawHat.Response.or_else(business_logic)
{:error, "a better error message"}
iex> default_value = fn _ -> {:ok, []} end
...> {:error, "oops"} |> StrawHat.Response.or_else(default_value)
{:ok, []}
reject_if(result, predicate, error_message \\ :invalid)
View Source
reject_if(result_tuple(), (any() -> boolean()), any()) :: result_tuple()
reject_if(result_tuple(), (any() -> boolean()), any()) :: result_tuple()
Converts an Ok
value to an Error
value if the predicate
is valid.
Examples
iex> res = StrawHat.Response.ok([])
...> StrawHat.Response.reject_if(res, &Enum.empty?/1)
{:error, :invalid}
iex> res = StrawHat.Response.ok([1])
...> StrawHat.Response.reject_if(res, &Enum.empty?/1)
{:ok, [1]}
iex> res = StrawHat.Response.ok([])
...> StrawHat.Response.reject_if(res, &Enum.empty?/1, "list cannot be empty")
{:error, "list cannot be empty"}
sequence(list)
View Source
sequence([result_tuple()]) :: {:ok, [any()]} | {:error, any()}
sequence([result_tuple()]) :: {:ok, [any()]} | {:error, any()}
Transforms a list of result tuple to a result tuple containing either the first error tuple or an ok tuple containing the list of values.
Examples
iex> StrawHat.Response.sequence([StrawHat.Response.ok(42), StrawHat.Response.ok(1337)])
{:ok, [42, 1337]}
iex> StrawHat.Response.sequence([StrawHat.Response.ok(42), StrawHat.Response.error("oops"), StrawHat.Response.ok(1337)])
{:error, "oops"}
tap(data, function)
View Source
tap(result_tuple(), (any() -> any())) :: result_tuple()
tap(result_tuple(), (any() -> any())) :: result_tuple()
Calls the next function only if it receives an ok tuple but discards the result. It always returns the original tuple.
Examples
iex> some_logging = fn x -> "Success #{x}" end
...> {:ok, 42} |> StrawHat.Response.tap(some_logging)
{:ok, 42}
iex> some_logging = fn _ -> "Not called logging" end
...> {:error, "oops"} |> StrawHat.Response.tap(some_logging)
{:error, "oops"}
tap_error(data, function)
View Source
tap_error(result_tuple(), (any() -> any())) :: result_tuple()
tap_error(result_tuple(), (any() -> any())) :: result_tuple()
Calls the next function only if it receives an error tuple but discards the result. It always returns the original tuple.
Examples
iex> some_logging = fn x -> "Failed #{x}" end ...> {:error, "oops"} |> StrawHat.Response.tap_error(some_logging)
iex> somelogging = fn -> "Not called logging" end ...> {:ok, 42} |> StrawHat.Response.tap_error(some_logging)
with_default(arg, default_data)
View Source
with_default(result_tuple(), any()) :: any()
with_default(result_tuple(), any()) :: any()
Returns the content of an ok tuple if the value is correct. Otherwise it returns the default value.
Examples
iex> 42 |> StrawHat.Response.ok |> StrawHat.Response.with_default(1337)
42
iex> "oops" |> StrawHat.Response.error |> StrawHat.Response.with_default(1337)
1337