View Source WuunderUtils.Results (Wuunder Utils v0.5.0)

A set of handy helpers to deal with {:ok, } or {:error, } tuples

Summary

Functions

Checks if all results errored

Checks if there are no errors and all results are {:ok, ...}

Tests if result contains an error

Flattens a result tuple. Specifcally flattens the second value in the tuple. Comes in handy when functions return a {:ok, } or {:error, } tuple with another tuple nested inside of it.

Returns the result from a list of results ({:ok, }, {:error, }). Will return {:ok, first_result} with the first result when all of the values in the list are :ok. If one of the results has an :error, it will return {:error, first_error_result}

Retrieves the first occurence of an error tuple in the result list

If an error is encountered in given value, it will return {:error, _}. The value can be a list or a single value

Retrieves all occurences of an error tuple in the result list

If an error is encountered in given value, it will return {:error, _}. The value can be a list or a single value or a list with errors.

Retrieve the first :ok result from a list of results. Returns the value of OK, not the tuple itself.

If an ok is encountered in given value, it will return {:ok, _}. The value can be a list or a single value. Note that it will only return first ok from a list.

Retrieve all :ok results from a list of results. Returns the values of OK, not the tuple itself.

If an ok is encountered in given value, it will return {:ok, _}. The value can be a list or a single value. It will return the entire list if the given value is a list.

Returns the values from a list with result tuples when all are OK Or return {:error, _} when an error has eccoured in one of the items

Grabs result code from tuple or value. Valid values are :ok, :error or {:ok, } or {:error, }

Retrieves the data is stored inside a {:ok, X} or {:error, Y} tuple. Only gets everything after the :ok or :error. All other values are left alone and just returned.

Retrieve the first success result from a list of results. Returns the original values. Will parse {:ok} tuples if needed.

Only returns ok results from a list. Besides :ok this means anything other than an error. So in practice: {:ok, } tuples, :ok, non :error / {:error, } values. Note that nills are also considered ok. And also note: an :ok will be transformed to nil.

Retrieve all success results from a list of results. Returns the values of a {:ok, x} or the original value.

Checks if any items in the list contains an error

Checks if there any OKs in the value. Could be a list or a single value.

Tests if result is OK

Checks if given value is :ok, :error or {:ok, } or {:error, }

Tests if result is a success. Meaning: :ok, {:ok, X} or any other value othen than {:error, x} or :error

Functions

@spec all_error?(term()) :: boolean()

Checks if all results errored

Examples

iex> WuunderUtils.Results.all_error?([
...>   {:error, %Shipment{id: 1}},
...>   {:error, %Shipment{id: 2}}
...> ])
true

iex> WuunderUtils.Results.all_error?(:error)
true

iex> WuunderUtils.Results.all_error?({:error, %Shipment{id: 2}})
true

iex> WuunderUtils.Results.all_error?([{:ok, %Shipment{id: 2}}, {:error, %Shipment{id: 3}}])
false
@spec all_ok?(term()) :: boolean()

Checks if there are no errors and all results are {:ok, ...}

Examples

iex> WuunderUtils.Results.all_ok?([
...>   {:ok, %Shipment{id: 1}},
...>   {:ok, %Shipment{id: 2}}
...> ])
true

iex> WuunderUtils.Results.all_ok?(:ok)
true

iex> WuunderUtils.Results.all_ok?({:ok, %Shipment{id: 2}})
true

iex> WuunderUtils.Results.all_ok?([{:ok, %Shipment{id: 2}}, {:error, %Shipment{id: 3}}])
false
@spec error?(any()) :: boolean()

Tests if result contains an error

Examples

iex> WuunderUtils.Results.error?({:error, "error message"})
true

iex> WuunderUtils.Results.error?({:ok, "value"})
false

iex> WuunderUtils.Results.error?([])
false
@spec flatten_result(any()) :: any()

Flattens a result tuple. Specifcally flattens the second value in the tuple. Comes in handy when functions return a {:ok, } or {:error, } tuple with another tuple nested inside of it.

Examples

iex> WuunderUtils.Results.flatten_result({:error, {:internal_error, :get_orders, "Internal Server Error"}})
{:error, :internal_error, :get_orders, "Internal Server Error"}

iex> WuunderUtils.Results.flatten_result({:error, {:internal_error, :get_orders}, {1, 2}})
{:error, :internal_error, :get_orders, {1, 2}}

iex> WuunderUtils.Results.flatten_result({:error, {:internal_error, :get_orders, [1, 2]}, {1, 2}})
{:error, :internal_error, :get_orders, [1, 2], {1, 2}}

iex> WuunderUtils.Results.flatten_result({:error, :internal_error, :get_orders})
{:error, :internal_error, :get_orders}

iex> WuunderUtils.Results.flatten_result(:error)
:error
@spec get_as_result(list() | term()) :: tuple()

Returns the result from a list of results ({:ok, }, {:error, }). Will return {:ok, first_result} with the first result when all of the values in the list are :ok. If one of the results has an :error, it will return {:error, first_error_result}

Example

iex> WuunderUtils.Results.get_as_result([{:ok, "value"}, {:ok, "value-2"}])
{:ok, "value"}

iex> WuunderUtils.Results.get_as_result([{:ok, "value"}, {:error, "value-2"}])
{:error, "value-2"}

iex> WuunderUtils.Results.get_as_result([{:error, "value"}, {:error, "value-2"}])
{:error, "value"}
@spec get_error(list() | term()) :: any()

Retrieves the first occurence of an error tuple in the result list

Examples

 iex> results = [
 ...>   {:ok, %Shipment{id: 1}},
 ...>   {:ok, %Shipment{id: 2}},
 ...>   {:error, :creation_error}
 ...> ]
 ...>
 ...> WuunderUtils.Results.get_error(results)
 :creation_error
Link to this function

get_error_as_result(results)

View Source
@spec get_error_as_result(any()) :: {:error, term()} | nil

If an error is encountered in given value, it will return {:error, _}. The value can be a list or a single value

Examples

 iex> WuunderUtils.Results.get_error_as_result([{:ok, "value"}, {:error, "something went wrong"}])
 {:error, "something went wrong"}

 iex> WuunderUtils.Results.get_error_as_result({:ok, "value"})
 nil

 iex> WuunderUtils.Results.get_error_as_result("some-value")
 nil
@spec get_errors(list() | term()) :: any()

Retrieves all occurences of an error tuple in the result list

Examples

 iex> results = [
 ...>   {:ok, %Shipment{id: 1}},
 ...>   {:ok, %Shipment{id: 2}},
 ...>   {:error, "other-error"},
 ...>   {:error, :creation_error}
 ...> ]
 ...>
 ...> WuunderUtils.Results.get_errors(results)
 ["other-error", :creation_error]
Link to this function

get_errors_as_result(results)

View Source
@spec get_errors_as_result(any()) :: {:error, list()} | nil

If an error is encountered in given value, it will return {:error, _}. The value can be a list or a single value or a list with errors.

Examples

 iex> WuunderUtils.Results.get_errors_as_result({:error, "some-value"})
 {:error, ["some-value"]}

 iex> WuunderUtils.Results.get_errors_as_result([{:error, "value-1"}, {:error, "value-2"}, {:ok, "value"}])
 {:error, ["value-1", "value-2"]}
@spec get_ok(any()) :: term() | nil

Retrieve the first :ok result from a list of results. Returns the value of OK, not the tuple itself.

Examples

 iex> results = [
 ...>   {:ok, %Shipment{id: 1}},
 ...>   {:ok, %Shipment{id: 2}},
 ...>   {:error, :creation_error}
 ...> ]
 ...>
 ...> WuunderUtils.Results.get_ok(results)
 %Shipment{id: 1}
Link to this function

get_ok_as_result(results)

View Source
@spec get_ok_as_result(any()) :: {:ok, any()} | nil

If an ok is encountered in given value, it will return {:ok, _}. The value can be a list or a single value. Note that it will only return first ok from a list.

Examples

   iex> WuunderUtils.Results.get_ok_as_result({:ok, "some-value"})
   {:ok, "some-value"}

   iex> WuunderUtils.Results.get_ok_as_result([{:ok, "value-1"}, {:error, "error"}, {:ok, "value-2"}])
   {:ok, "value-1"}

   iex> WuunderUtils.Results.get_ok_as_result({:error, "value"})
   nil
@spec get_oks(list() | term()) :: list()

Retrieve all :ok results from a list of results. Returns the values of OK, not the tuple itself.

Examples

 iex> results = [
 ...>   {:ok, %Shipment{id: 1}},
 ...>   {:ok, %Shipment{id: 2}},
 ...>   {:error, :creation_error}
 ...> ]
 ...>
 ...> WuunderUtils.Results.get_oks(results)
 [%Shipment{id: 1}, %Shipment{id: 2}]
Link to this function

get_oks_as_result(results)

View Source
@spec get_oks_as_result(list() | term()) :: {:ok, list()} | nil

If an ok is encountered in given value, it will return {:ok, _}. The value can be a list or a single value. It will return the entire list if the given value is a list.

Examples

iex> WuunderUtils.Results.get_oks_as_result({:ok, "some-value"})
{:ok, ["some-value"]}

iex> WuunderUtils.Results.get_oks_as_result([{:ok, "value-1"}, {:ok, "value-2"}])
{:ok, ["value-1", "value-2"]}

iex> WuunderUtils.Results.get_oks_as_result({:error, "value"})
[]
Link to this function

get_oks_or_error_result(results)

View Source
@spec get_oks_or_error_result(any()) :: any()

Returns the values from a list with result tuples when all are OK Or return {:error, _} when an error has eccoured in one of the items

Examples

   iex> WuunderUtils.Results.get_oks_or_error_result([{:ok, "value"}, {:ok, "value-2"}, :ok])
   ["value", "value-2", nil]

   iex> WuunderUtils.Results.get_oks_or_error_result([{:ok, "value"}, {:ok, "value-2"}, {:error, "faulty"}])
   {:error, "faulty"}
@spec get_result_code(any()) :: :ok | :error | nil

Grabs result code from tuple or value. Valid values are :ok, :error or {:ok, } or {:error, }

Examples

iex> WuunderUtils.Results.get_result_code({:ok, "value"})
:ok

iex> WuunderUtils.Results.get_result_code(:ok)
:ok

iex> WuunderUtils.Results.get_result_code({:error, "value", "whatever"})
:error

iex> WuunderUtils.Results.get_result_code(:error)
:error

iex> WuunderUtils.Results.get_result_code("value")
nil
@spec get_result_value(any()) :: any() | nil

Retrieves the data is stored inside a {:ok, X} or {:error, Y} tuple. Only gets everything after the :ok or :error. All other values are left alone and just returned.

Examples

iex> WuunderUtils.Results.get_result_value({:ok, "value-1"})
"value-1"

iex> WuunderUtils.Results.get_result_value({:ok, "value-1", "value-2"})
{"value-1", "value-2"}

iex> WuunderUtils.Results.get_result_value({:error, :internal_server_error})
:internal_server_error

iex> WuunderUtils.Results.get_result_value({:error, :error_a, :error_b, :error_c})
{:error_a, :error_b, :error_c}

iex> WuunderUtils.Results.get_result_value("any-value")
"any-value"

iex> WuunderUtils.Results.get_result_value(:ok)
nil

iex> WuunderUtils.Results.get_result_value(:error)
nil
@spec get_success(term()) :: term() | nil

Retrieve the first success result from a list of results. Returns the original values. Will parse {:ok} tuples if needed.

Examples

iex> results = [
...>   {:ok, %Shipment{id: 1}},
...>   {:ok, %Shipment{id: 2}},
...>   "some-value",
...>   nil,
...>   {:error, :creation_error}
...> ]
...>
...> WuunderUtils.Results.get_success(results)
%Shipment{id: 1}
Link to this function

get_success_values(values)

View Source
@spec get_success_values(list() | term()) :: list()

Only returns ok results from a list. Besides :ok this means anything other than an error. So in practice: {:ok, } tuples, :ok, non :error / {:error, } values. Note that nills are also considered ok. And also note: an :ok will be transformed to nil.

Examples

iex> results = [
...>   {:ok, "value1"},
...>   :ok,
...>   {:ok, "value2", "value3"},
...>   {:ok, {"value4", "value5"}},
...>   {:error, "something went wrong"},
...>   nil
...> ]
...>
...> WuunderUtils.Results.get_success_values(results)
["value1", nil, {"value2", "value3"}, {"value4", "value5"}, nil]
@spec get_successes(list() | term()) :: list()

Retrieve all success results from a list of results. Returns the values of a {:ok, x} or the original value.

Examples

iex> results = [
...>   {:ok, %Shipment{id: 1}},
...>   {:ok, %Shipment{id: 2}},
...>   %Shipment{id: 3},
...>   {:error, :creation_error}
...> ]
...>
...> WuunderUtils.Results.get_successes(results)
[%Shipment{id: 1}, %Shipment{id: 2}, %Shipment{id: 3}]
@spec has_error?(term()) :: boolean()

Checks if any items in the list contains an error

Examples

iex> WuunderUtils.Results.has_error?([
...>   {:ok, %Shipment{id: 1}},
...>   {:ok, %Shipment{id: 2}},
...>   {:error, :creation_error},
...>   :error
...> ])
true

iex> WuunderUtils.Results.has_error?(:error)
true

iex> WuunderUtils.Results.has_error?({:error, :creating_error})
true

iex> WuunderUtils.Results.has_error?({:ok, %Shipment{id: 1}})
false
@spec has_ok?(term()) :: boolean()

Checks if there any OKs in the value. Could be a list or a single value.

Examples

iex> WuunderUtils.Results.has_ok?(:ok)
true

iex> WuunderUtils.Results.has_ok?({:ok, "hello world"})
true

iex> WuunderUtils.Results.has_ok?([{:ok, "hello world"}, {:error, :faulty}])
true

iex> WuunderUtils.Results.has_ok?([{:error, "connection lost"}, {:error, :faulty}])
false
@spec ok?(any()) :: boolean()

Tests if result is OK

Examples

iex> WuunderUtils.Results.ok?({:ok, "value"})
true

iex> WuunderUtils.Results.ok?(:ok)
true

iex> WuunderUtils.Results.ok?("some-value")
false

iex> WuunderUtils.Results.ok?({:error, "error message"})
false

iex> WuunderUtils.Results.ok?([])
false
@spec result?(any()) :: boolean()

Checks if given value is :ok, :error or {:ok, } or {:error, }

Examples

iex> WuunderUtils.Results.result?({:ok, "value"})
true

iex> WuunderUtils.Results.result?({:error, "value"})
true

iex> WuunderUtils.Results.result?(:ok)
true

iex> WuunderUtils.Results.result?(:error)
true

iex> WuunderUtils.Results.result?("value")
false
@spec success?(any()) :: boolean()

Tests if result is a success. Meaning: :ok, {:ok, X} or any other value othen than {:error, x} or :error

Examples

iex> WuunderUtils.Results.success?({:ok, "value"})
true

iex> WuunderUtils.Results.success?([])
true

iex> WuunderUtils.Results.success?(nil)
true

iex> WuunderUtils.Results.success?({:error, "error message"})
false