View Source WuunderUtils.Results (Wuunder Utils v0.9.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, ...}
Wraps the given value in an error tuple.
Tests if result contains an error
Flattens a result tuple. Comes in handy when functions return a {:ok, } or {:error, } tuple with another set of tuples nested inside of it.
Retrieves the first occurence of an error tuple in the result list
Retrieves all occurences of an error tuple in the result list
Retrieve the first :ok result from a list of results. Returns the value of OK, not the tuple itself.
Retrieve all :ok results from a list of results. Returns the values of OK, not the tuple itself.
Returns error or ok from result list. Gives priority to error in list.
Gets a list of errors or a list of ok's. Gives the priority to list of errors
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.
Grabs the values of a list of result tuples
Checks if any items in the list contains an error
Checks if there any OK results in the list.
Wraps the given value in an ok tuple.
Tests if result is OK
Checks if given value is :ok, :error or {:ok, } or {:error, }
Types
@type error() :: :error
@type ok() :: :ok
@type result() :: ok() | error() | ok_tuple() | error_tuple()
@type result_list() :: [result()]
Functions
@spec all_error?(result_list()) :: 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?([{:ok, %Shipment{id: 2}}, {:error, %Shipment{id: 3}}])
false
@spec all_ok?(result_list()) :: 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, %Shipment{id: 2}}, {:error, %Shipment{id: 3}}])
false
Wraps the given value in an error tuple.
Examples
iex> WuunderUtils.Results.error("value")
{:error, "value"}
Tests if result contains an error
Examples
iex> WuunderUtils.Results.error?({:error, "error message"})
true
iex> WuunderUtils.Results.error?(:error)
true
iex> WuunderUtils.Results.error?({:error})
false
iex> WuunderUtils.Results.error?({:ok, "value"})
false
iex> WuunderUtils.Results.error?([])
false
Flattens a result tuple. Comes in handy when functions return a {:ok, } or {:error, } tuple with another set of tuples nested inside of it.
Examples
iex> WuunderUtils.Results.flatten({:error, {:internal_error, :get_orders, "Internal Server Error"}})
{:error, :internal_error, :get_orders, "Internal Server Error"}
iex> WuunderUtils.Results.flatten({:error, {:internal_error, :get_orders}, {1, 2}})
{:error, :internal_error, :get_orders, 1, 2}
iex> WuunderUtils.Results.flatten({:error, {:internal_error, :get_orders, [1, 2]}, {1, 2}})
{:error, :internal_error, :get_orders, [1, 2], 1, 2}
iex> WuunderUtils.Results.flatten({:error, :internal_error, :get_orders})
{:error, :internal_error, :get_orders}
iex> WuunderUtils.Results.flatten(:error)
:error
@spec get_error(result_list()) :: result() | nil
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)
{:error, :creation_error}
iex> results = [
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> ]
...>
...> WuunderUtils.Results.get_error(results)
nil
@spec get_errors(result_list()) :: result_list()
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)
[{:error, "other-error"}, {:error, :creation_error}]
@spec get_ok(result_list()) :: result() | 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)
{:ok, %Shipment{id: 1}}
iex> results = [
...> {:error, :creation_error}
...> ]
...>
...> WuunderUtils.Results.get_ok(results)
nil
@spec get_oks(result_list()) :: result_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)
[{:ok, %WuunderUtils.ResultsTest.Shipment{id: 1, weight: 0}}, {:ok, %WuunderUtils.ResultsTest.Shipment{id: 2, weight: 0}}]
@spec get_result(result_list()) :: result()
Returns error or ok from result list. Gives priority to error in list.
Examples
iex> WuunderUtils.Results.get_result([
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> {:error, :creation_error},
...> :error
...> ])
{:error, :creation_error}
iex> WuunderUtils.Results.get_result([
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> ])
{:ok, %Shipment{id: 1}}
@spec get_results(result_list()) :: result_list()
Gets a list of errors or a list of ok's. Gives the priority to list of errors
Examples
iex> WuunderUtils.Results.get_results([
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> {:error, :creation_error},
...> :error
...> ])
[{:error, :creation_error}, :error]
iex> WuunderUtils.Results.get_results([
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> ])
[{:ok, %Shipment{id: 1}}, {:ok, %Shipment{id: 2}}]
@spec get_value(result()) :: term()
@spec get_value(result_list()) :: term()
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_value({:ok, "value-1"})
"value-1"
iex> WuunderUtils.Results.get_value({:ok, "value-1", "value-2"})
{"value-1", "value-2"}
iex> WuunderUtils.Results.get_value({:error, :internal_server_error})
:internal_server_error
iex> WuunderUtils.Results.get_value({:error, :error_a, :error_b, :error_c})
{:error_a, :error_b, :error_c}
iex> WuunderUtils.Results.get_value(:ok)
nil
iex> WuunderUtils.Results.get_value(:error)
nil
Grabs the values of a list of result tuples
Examples
iex> WuunderUtils.Results.get_values([{:ok, "value-1"}])
["value-1"]
iex> WuunderUtils.Results.get_values([{:ok, "value-1", "value-2"}, {:ok, "value-3"}])
[{"value-1", "value-2"}, "value-3"]
iex> WuunderUtils.Results.get_values([{:error, :internal_server_error}])
[:internal_server_error]
iex> WuunderUtils.Results.get_values([:ok, :error])
[nil, nil]
@spec has_error?(result_list()) :: 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?([
...> {:ok, %Shipment{id: 1}},
...> {:ok, %Shipment{id: 2}},
...> :ok
...> ])
false
@spec has_ok?(result_list()) :: boolean()
Checks if there any OK results in the list.
Examples
iex> WuunderUtils.Results.has_ok?([{:ok, "hello world"}, {:error, :faulty}])
true
iex> WuunderUtils.Results.has_ok?([{:error, "connection lost"}, {:error, :faulty}])
false
Wraps the given value in an ok tuple.
Examples
iex> WuunderUtils.Results.ok("value")
{:ok, "value"}
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
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?({:error})
false
iex> WuunderUtils.Results.result?({:ok})
false
iex> WuunderUtils.Results.result?("value")
false