Exceptional v2.1.3 Exceptional.Value View Source

Provide an escape hatch for propagating unraised exceptions

Convenience uses

Everything:

use Exceptional.Value

Only named functions (exception_or_continue):

use Exceptional.Value, only: :named_functions

Only operators (~>):

use Exceptional.Value, only: :operators

Link to this section Summary

Functions

If an exception, return the exception, otherwise continue computation. Essentially an Either construct for Exceptions

Operator alias for exception_or_continue

Link to this section Functions

Link to this macro

exception_or_continue(maybe_exception, continue) View Source (macro)
exception_or_continue(Exception.t() | any(), (... -> any())) ::
  Exception.t() | any()

If an exception, return the exception, otherwise continue computation. Essentially an Either construct for Exceptions.

Note that this does not rescue raises. If you want that behaviour, please see Exceptional.Safe.safe/1

Examples

iex> 1 |> exception_or_continue(fn value -> value * 100 end.())
100

iex> %ArgumentError{message: "exception handled"}
...> |> exception_or_continue(fn value -> value * 100 end.())
%ArgumentError{message: "exception handled"}

iex> %ArgumentError{message: "exception handled"}
...> |> exception_or_continue(fn x -> x + 1 end.())
...> |> exception_or_continue(fn y -> y - 10 end.())
%ArgumentError{message: "exception handled"}

iex> %ArgumentError{message: "exception not caught"}
...> |> raise
...> |> exception_or_continue(fn value -> value * 100 end.())
** (ArgumentError) exception not caught

iex> Enum.fetch!([], 9) |> exception_or_continue(fn v -> v * 10 end.())
** (Enum.OutOfBoundsError) out of bounds error
Link to this macro

maybe_exception ~> continue View Source (macro)

Operator alias for exception_or_continue

Examples

iex> 1 ~> fn value -> value * 100 end.()
100

iex> exception = %Enum.OutOfBoundsError{message: "exception"}
...> exception ~> fn x -> x + 1 end.()
%Enum.OutOfBoundsError{message: "exception"}

...> exception
...> ~> fn x -> x + 1 end.()
...> ~> fn y -> y - 10 end.()
%Enum.OutOfBoundsError{message: "exception"}

...> raise(exception) ~> fn x -> x + 1 end.()
** (Enum.OutOfBoundsError) out of bounds error

iex> Enum.fetch!([], 9) ~> fn x -> x + 1 end.()
** (Enum.OutOfBoundsError) out of bounds error