StrawHat v0.6.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

Link to this type

error_tuple()

View Source
error_tuple() :: {:error, any()}
Link to this type

ok_tuple()

View Source
ok_tuple() :: {:ok, any()}
Link to this type

result_tuple()

View Source
result_tuple() :: ok_tuple() | error_tuple()
Link to this type

t(ok, error)

View Source
t(ok, error) :: {:ok, ok} | {:error, error}

Link to this section Functions

Link to this function

and_then(error, function)

View Source
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"}
Link to this function

either(arg, on_error, on_ok)

View Source
either(result_tuple(), (any() -> any()), (any() -> any())) :: any()

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"

Creates a new error result tuple.

Examples

iex> StrawHat.Response.error("oops")
{:error, "oops"}

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
Link to this function

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"}
Link to this function

keep_if(result, predicate, error_message \\ :invalid)

View Source
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}
Link to this function

map(error, function)

View Source
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"}
Link to this function

map_error(data, function)

View Source
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"}

Creates a new ok result tuple.

Examples

iex> StrawHat.Response.ok(42)
{:ok, 42}

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
Link to this function

or_else(data, function)

View Source
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, []}
Link to this function

reject_if(result, predicate, error_message \\ :invalid)

View Source
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"}
Link to this function

sequence(list)

View Source
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"}
Link to this function

tap(data, function)

View Source
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"}
Link to this function

tap_error(data, function)

View Source
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)

Link to this function

with_default(arg, default_data)

View Source
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