From 51077765e589603c8d776546589a53105bf966f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Sat, 6 Jul 2024 03:37:11 +0200 Subject: [PATCH] mapEffectErrorsToTRPC --- .../server/src/trpc/TRPCContextCreator.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/server/src/trpc/TRPCContextCreator.ts b/packages/server/src/trpc/TRPCContextCreator.ts index 5aaa1fe..bb8f3bd 100644 --- a/packages/server/src/trpc/TRPCContextCreator.ts +++ b/packages/server/src/trpc/TRPCContextCreator.ts @@ -1,3 +1,4 @@ +import { TRPCError } from "@trpc/server" import type { CreateExpressContextOptions } from "@trpc/server/adapters/express" import { Context, Effect, Layer, Runtime } from "effect" import type { Services } from "../Services" @@ -23,3 +24,21 @@ export module TRPCContextCreator { }) })) } + + +const mapEffectErrorsToTRPC = (effect: Effect.Effect) => + Effect.sandbox(effect).pipe( + Effect.catchTags({ + Die: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR" }) + ), + + Interrupt: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR" }) + ), + + Fail: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR" }) + ), + }) + )