Sandboxing
sandbox lifts the complete runtime failure cause into the typed error channel, changing A!E!R to A!Cause.Cause<E>!R. Use it when a boundary pipeline must inspect or handle defects, interruption, and expected failures as explicit typed error data.
| Operation | Effect on E | When to use |
|---|---|---|
sandbox(eff) | E becomes Cause.Cause<E> | Multi-step inspection or error-channel pipelines over full causes |
failCause(cause) | Restores original E, defects, and interrupts | Re-emitting an unhandled sandboxed cause faithfully |
fail(cause) / throw cause | Keeps Cause<E> as typed error data | Passing a cause value onward as a normal error |
catchCause(eff, handler) | Unchanged outside handler | Single-step cause inspection or recovery without lifting E |
Moving causes into the error channel
error InvalidRow {
line: number
}
effect function readRow() {
throw new InvalidRow({ line: 8 })
}
effect function sandboxedRow() {
return try sandbox(readRow())
}
// never!Cause.Cause<InvalidRow>
// Run in an effect function:
// try Console.log((try flip(readRow())).line)
// {
// const jsonValue = (try flip(sandboxedRow())).reasons.map((reason) => reason._tag)
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }8
["Fail"]Cause is in scope in .vibe files without an import. Inside sandbox, a defect in the wrapped computation becomes a Die reason inside the typed Cause value rather than crashing the outer computation.
Inspecting individual reasons
effect function rowFailureKinds() {
return sandboxedRow() catch (cause) => cause.reasons.map((reason) => {
if (Cause.isFailReason(reason)) return `row ${reason.error.line}`
if (Cause.isDieReason(reason)) return "defect"
return "interruption"
})
}
// Run in an effect function:
// {
// const jsonValue = try rowFailureKinds()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }["row 8"]Effect 4 stores failure reasons in a flat readonly array (cause.reasons), and one outcome can contain several. Narrow each entry with Cause.isFailReason (reason.error), Cause.isDieReason (reason.defect), or Cause.isInterruptReason.
Recovering selected variants
effect function rowOrPlaceholder() {
return sandboxedRow() catch effect (cause) => {
if (!Cause.hasDies(cause) && !Cause.hasInterrupts(cause)) {
return "Malformed row omitted"
}
return try failCause(cause)
}
}
// Run in an effect function: try Console.log(try rowOrPlaceholder())Malformed row omittedThis handler recovers only causes composed strictly of expected domain failures. Checking only for a Fail reason would also swallow defects or interruption in a mixed cause; sandboxing forces that policy to be explicit.
Restoring causes after inspection
effect function readWithInspection(): never!InvalidRow {
return sandboxedRow() catch effect (cause) => {
const kinds = cause.reasons.map((reason) => reason._tag)
try Console.log("Row computation stopped", try orDie(Effect.try(() => JSON.stringify(kinds))))
return try failCause(cause)
}
}
// never!InvalidRow
// Run in an effect function: try Console.log((try flip(readWithInspection())).line)Row computation stopped ["Fail"]
8failCause(cause) restores every expected failure, defect, and interruption to its original runtime channel, whereas fail(cause) or throw cause leaves the Cause object as typed error data. For one-off inspection or recovery, catchCause(readRow(), handler) avoids rewriting E first.
The log records only reason tags, and orDie keeps a serialization bug out of the restored domain error channel.
The matching Effect page: effect.website/docs/v4/error-management/sandboxing