Skuld.Comp.ConvertThrow (skuld v0.1.13)

View Source

Utilities 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

handle_exception(kind, e, stacktrace, env)

@spec handle_exception(atom(), term(), list(), map()) :: {term(), map()}

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(expr)

(macro)

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

wrap_expr(expr)

@spec wrap_expr(Macro.t()) :: Macro.t()

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:

  1. Defers evaluation of the expression until inside the computation context
  2. Catches any exceptions (raise/throw/exit) during evaluation
  3. 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