Skuld.Comp.ConvertThrow (skuld v0.1.13)
View SourceUtilities for converting Elixir exceptions to Throw effects.
Provides a macro and helper function for wrapping code in try/catch that converts exceptions (raise/throw/exit) to Skuld Throw effects.
Summary
Functions
Convert an exception to a Throw result.
Wrap an expression in a computation that catches exceptions and converts them to Throw effects.
Generate AST that wraps an expression in exception-handling computation.
Functions
Convert an exception to a Throw result.
Re-raises InvalidComputation errors (programming bugs that should fail fast).
Other exceptions are wrapped in a Throw struct and passed through leave_scope.
Wrap an expression in a computation that catches exceptions and converts them to Throw effects.
This is used by the comp macro to wrap the first expression, ensuring
that exceptions raised during evaluation are properly converted.
Example
# Instead of:
Skuld.Comp.bind(Risky.boom!(), fn x -> x + 1 end)
# Which raises before bind is called
# Generate:
Skuld.Comp.bind(
Skuld.Comp.ConvertThrow.wrap(Risky.boom!()),
fn x -> x + 1 end
)
# Which catches the exception and converts to Throw
Generate AST that wraps an expression in exception-handling computation.
This is the underlying implementation used by both the wrap/1 macro and
the comp macro in CompBlock. It generates a computation function that:
- Defers evaluation of the expression until inside the computation context
- Catches any exceptions (raise/throw/exit) during evaluation
- Converts them to Skuld Throw effects via
handle_exception/4
Parameters
expr- The quoted expression AST to wrap
Returns
Quoted AST for a computation function fn env, k -> ... end