Effect CommunityEC
Effect Communityโ€ข3y agoโ€ข
5 replies
imagio

Mapping Errors while Preserving Original Error: Handling Errors in TypeScript

What pattern do you use when you want to map an error to another type but preserve the original error? For example

class SaveFailedError extends Data.TaggedError("SaveFailedError")<{
    
}> {}

pipe(
   doSaveEffect(),
   Effect.retryN(5),
   // how do you handle this while preserving the original error?
   Effect.mapError(e => new SaveFailedError({})
)


Adding a field to SaveFailedError doesn't seem ideal because there's no guarantee that all my errors conform to that shape which makes general error processing (logging, reporting to sentry) more difficult. It seems like a job for Cause but I'm not sure how best to use it.

Is there an established pattern for this case?
Was this page helpful?