Sandboxing
Errors are an inevitable part of programming, and they can arise from various sources like failures, defects, fiber interruptions, or combinations of these. This guide explains how to use the Effect.sandbox
function to isolate and understand the causes of errors in your Effect-based code.
The Effect.sandbox
function allows you to encapsulate all the potential causes of an error in an effect. It exposes the full cause of an effect, whether it’s due to a failure, defect, fiber interruption, or a combination of these factors.
In simple terms, it takes an effect Effect<A, E, R>
and transforms it into an effect Effect<A, Cause<E>, R>
where the error channel now contains a detailed cause of the error.
Syntax
By using the Effect.sandbox
function, you gain access to the underlying causes of exceptional effects. These causes are represented as a type of Cause<E>
and are available in the error channel of the Effect
data type.
Once you have exposed the causes, you can utilize standard error-handling operators like Effect.catchAll and Effect.catchTags to handle errors more effectively. These operators allow you to respond to specific error conditions.
If needed, we can undo the sandboxing operation with Effect.unsandbox
.
Example (Handling Different Error Causes)