| title | Understand the Cause Data Type | ||||||
|---|---|---|---|---|---|---|---|
| id | data-cause | ||||||
| skillLevel | advanced | ||||||
| applicationPatternId | core-concepts | ||||||
| summary | Use Cause<E> to get rich, structured information about errors and failures, including defects, interruptions, and error traces. | ||||||
| tags |
|
||||||
| rule |
|
||||||
| related |
|
||||||
| author | PaulJPhilp | ||||||
| lessonOrder | 2 |
Use the Cause<E> data type to get rich, structured information about errors and failures in your Effects.
Cause captures not just expected errors, but also defects (unhandled exceptions), interruptions, and error traces.
Traditional error handling often loses information about why a failure occurred.
Cause preserves the full error context, enabling advanced debugging, error reporting, and robust recovery strategies.
import { Cause, Effect } from "effect";
// An Effect that may fail with an error or defect
const program = Effect.try({
try: () => {
throw new Error("Unexpected failure!");
},
catch: (err) => err,
});
// Catch all causes and inspect them
const handled = program.pipe(
Effect.catchAllCause((cause) =>
Effect.sync(() => {
if (Cause.isDie(cause)) {
console.error("Defect (die):", Cause.pretty(cause));
} else if (Cause.isFailure(cause)) {
console.error("Expected error:", Cause.pretty(cause));
} else if (Cause.isInterrupted(cause)) {
console.error("Interrupted:", Cause.pretty(cause));
}
// Handle or rethrow as needed
})
)
);Explanation:
Causedistinguishes between expected errors (fail), defects (die), and interruptions.- Use
Cause.prettyfor human-readable error traces. - Enables advanced error handling and debugging.
Catching only expected errors and ignoring defects or interruptions, which can lead to silent failures, missed bugs, and harder debugging.