View Source WuunderUtils.Results (Wuunder Utils v0.4.3)
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
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
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
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
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
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"}
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
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
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]
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"]}
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}
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
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}]
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"})
[]
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
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
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}
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]
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}]
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
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
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?("value")
false
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