Handling Errors in TypeScript with Effect and Async

Is this a heresy?

pipe(
  Effect.promise(() => myFunc()),
  Effect.catchAllDefect(error => {
    if (error instanceof SomeExpectedError) {
      return Effect.fail(new MyTaggedError({ cause: error }))
    }
    if (error instanceof SomeErrorToIgnore) {
      return Effect.succeed(defaultValue)
    }
    return Effect.die(error)
  }),
)


tryPromise doesn't let allow recovering from erros or dying. I've seen async recommended such use cases, but having to annotate the type manually is annying:

Effect.async<string, MyTaggedError>(resume => {
  myFunc()
    .then(value => resume(Effect.succeed(value)))
    .catch(error => {
      if (error instanceof SomeExpectedError) {
        resume(Effect.fail(new MyTaggedError({ cause: error })))
      } else if (error instanceof SomeErrorToIgnore) {
        resume(Effect.succeed(defaultValue))
      } else {
        resume(Effect.die(error))
      }
    })
})
Was this page helpful?