Effect CommunityEC
Effect Community7mo ago
4 replies
zino

What's the proper way to catchTags with exhaustive check?

I have an effect and I want to make sure every known tagged error from the error channel is handled.

With Match, i can do something like
Match.value(myEffect).pipe(
  Match.when("error 1", ...),
  Match.when("error 2", ...),
  Match.exhaustive
)

Is there a way to do exhaustive check when using Effect.catchTags?

This is what I'm doing right now:
const output = yield* fnThatReturnsAnEffect().pipe(
  Effect.catchTags({
    TaggedError1: () => Effect.succeed(null),
    TaggedError2: Effect.die,
  }),
  // this ensures all known error types have been handled
  Effect.mapError((error) => Match.value(error).pipe(Match.exhaustive))
);

The goal here is that if fnThatReturnsAnEffect ever returns a new type of error, i'll get a type failure here because that new error type won't be explicitly handled. The code i have ^^ seems to work fine. I just want to know if i need to do this everywhere or if there's a more "effect" or elegant way of doing this.

Many thanks in advance.
Was this page helpful?