Effect CommunityEC
Effect Community2y ago
4 replies
simon21

Handling Multiple Errors in a Similar Way

Question: What if you wanted to handle two errors the same way, how would you go about doing that?
Something along the lines of maybe?:
const recovered = program.pipe(
  Effect.catchTag(["FooError", "BarError"], (_fooError: FooError | BarError) =>
    Effect.succeed("Recovering the same way")
  )
)


The docs suggests two ways that both introduce code duplication (if you dont abstract the logic out in a function). But if the logic I want to not have duplicated is something as simple as e.g. logging the error, then it's overkill having to define a function for it:
const recovered = program.pipe(
  Effect.catchTag("FooError", (_fooError) =>
    Effect.succeed("Recovering the same way")      // <-- code duplication
  ),
  Effect.catchTag("BarError", (_barError) =>
    Effect.succeed("Recovering the same way")      // <-- code duplication
  )
)

const recovered = program.pipe(
  Effect.catchTags({
    FooError: (_fooError) => Effect.succeed(`Recovering in the same way`),  // <-- code duplication
    BarError: (_barError) => Effect.succeed(`Recovering in the same way`)  // <-- code duplication
  })
)

Note: Maybe I wont ever run into this in practice, it was just something I thought about when reading the docs
A fully-fledged functional effect system for TypeScript with a rich standard library
Expected Errors – Effect
Was this page helpful?