Reusable Error Handling with `catchTags` in TypeScript

I'm attempting to reuse common error handling with catchTags by separating the effect into its own variable:

export const handlePrismaError = (error: unknown) => {
  // handle prisma errors
};

// discriminate prisma error types, and recover
export const handlePrismaErrorEffect = Effect.catchTags({
  PrismaClientInitializationError: (error) => Effect.succeed({ status: 'error', error } as const),
  PrismaClientKnownRequestError: (error) => Effect.succeed({ status: 'error', error } as const),
  PrismaClientRustPanicError: (error) => Effect.succeed({ status: 'error', error } as const),
  PrismaClientUnknownRequestError: (error) => Effect.succeed({ status: 'error', error } as const),
  PrismaClientValidationError: (error) => Effect.succeed({ status: 'error', error } as const),
  UnknownError: (error) => Effect.succeed({ status: 'error', error } as const),
});

const fetchData = (
  slug: string,
): Effect.Effect<SuccessOrError<Maybe<...>>, ReturnType<typeof handlePrismaError>> =>
  Effect.tryPromise({
    try: () => prisma.page.findFirst({ where: { slug } }).then((data) => ({ status: 'success', data }) as const),
    catch: handlePrismaError,
  }).pipe(handlePrismaErrorEffect); // I would assume this would work


I've tried taking some inspiration from this thread...
effect-beginners-🚀

...but I've caught a case of the dumb. The example in that thread uses
catchTag
.
Was this page helpful?