Handling and logging exceptions in a functional effect system like Effect can be nuanced. Your cu...

Is there a better way / best practice approach to handling this?

export const mapUnknownException = <A, E>(effect: Effect.Effect<A, E>) => {
  return effect.pipe(
    Effect.tapErrorCause((e) => Effect.logError(e)),
    Effect.mapError((e) => Effect.gen(function* () {
        yield* Effect.logError(e)

        if (e instanceof UnknownException) {
          return new InternalError();
        }
        return e;
      
    }))
  );
}


What I'm trying to do:
- Catch any unknown exceptions from third-party libraries
- Provide sufficient logging for diagnosing should it come up in production
- Map to an InternalError which has a status code 500 annotation

Currently I'm logging all error causes but it would be good of I can conditionally log error causes when the error is of type UnknownException - haven't quite figured out how to do this yet
Was this page helpful?