Effect.ts: Absence as First-Class

Error HandlingLesson 6 of 51

6. HOW #1: Error Recovery

The SAME absence can be resolved different WAYS - here's fallback

Code Example
const risky = Effect.fail("Database down!");

// HOW: Provide fallback value
const safe = risky.pipe(
  Effect.catchAll(() =>
    Effect.succeed("Using cached data")
  )
);

// Type changed!
// Effect<string, never, never>
//                ^^^^^ No more errors!
Interactive Example
const risky = Effect.fail('Database down!');
const safe = risky.pipe(
      Effect.catchAll(() => Effect.succeed('Using cached data'))
);
const result = await Effect.runPromise(safe);
return `Error recovered!

Original: Would fail with "Database down!"
With catchAll: "${result}"

✓ Error type removed from signature!`;
Explanation

Handling unavailability without assumptions about success!

Traditional code assumes the database is available, then crashes. Effect TYPES the unavailability and lets you handle it:

  • risky: types unavailability that can fail
  • catchAll: types HOW to handle that unavailability
  • Result: unavailability that ALWAYS resolves (no error type)

We're not assuming data is available. We're describing: "If data is unavailable due to error, here's alternative unavailable data to use instead."


Part 6 of 51 in the Effect.ts Absence Modeling series