import { Effect, type Cause } from "effect" import { importTRPCServer } from "./importTRPCServer" export const createTRCPErrorMapper = importTRPCServer.pipe(Effect.map(({ TRPCError }) => (effect: Effect.Effect) => Effect.sandbox(effect).pipe( Effect.catchTags({ Empty: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), Fail: cause => Effect.fail( cause.error instanceof TRPCError ? cause.error : new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), Die: cause => Effect.fail( cause.defect instanceof TRPCError ? cause.defect : new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), Interrupt: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), Sequential: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), Parallel: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRPCErrorCause(cause), }) ), }), Effect.tapError(Effect.logError), ) )) class TRPCErrorCause extends Error { constructor(readonly cause: Cause.Cause) { super() } }