View Source StrawHat.Response (StrawHat v0.6.2)
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
Specs
error_tuple() :: {:error, any()}
Specs
ok_tuple() :: {:ok, any()}
Specs
result_tuple() :: ok_tuple() | error_tuple()
Specs
t(ok, error) :: {:ok, ok} | {:error, error}
Link to this section Functions
Specs
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"}
Specs
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"
Specs
error(any()) :: error_tuple()
Creates a new error result tuple.
Examples
iex> StrawHat.Response.error("oops")
{:error, "oops"}
Specs
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
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"}
Specs
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}
Specs
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"}
Specs
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"}
Specs
Creates a new ok result tuple.
Examples
iex> StrawHat.Response.ok(42)
{:ok, 42}
Specs
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
Specs
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, []}
Specs
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"}
Specs
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"}
Specs
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"}
Specs
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)
Specs
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