Exceptional v1.5.2 Exceptional.Value

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

Summary

Macros

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

Operator alias for exception_or_continue

Macros

exception_or_continue(maybe_exception, continue)
exception_or_continue(term, 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 catch raise or throws. If you want that behaviour, please see Exceptional.Rescue.

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
maybe_exception ~> continue

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