From 63f636f23173aab6d49d8d5fe37071afc742f268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Mon, 15 Jul 2024 02:06:17 +0200 Subject: [PATCH] tRPC context work --- .../server/src/trpc/TRPCContextCreator.ts | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/packages/server/src/trpc/TRPCContextCreator.ts b/packages/server/src/trpc/TRPCContextCreator.ts index 3a104c2..daa8c06 100644 --- a/packages/server/src/trpc/TRPCContextCreator.ts +++ b/packages/server/src/trpc/TRPCContextCreator.ts @@ -1,7 +1,7 @@ import { TRPCError } from "@trpc/server" import type { CreateExpressContextOptions } from "@trpc/server/adapters/express" import type { CreateWSSContextFnOptions } from "@trpc/server/adapters/ws" -import { Context, Effect, Layer, Runtime } from "effect" +import { Cause, Context, Effect, Layer, Runtime } from "effect" import type { Services } from "../Services" import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransaction } from "./TRPCContext" @@ -10,9 +10,9 @@ import { TRPCContextTransactionEnum, type TRPCContext, type TRPCContextTransacti * Provides functions that instantiate a fresh context for each tRPC procedure call */ export class TRPCContextCreator extends Context.Tag("TRPCContextCreator") TRPCContext - createExpressContext: (context: CreateExpressContextOptions) => TRPCContext - createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext + createContext: (transaction: TRPCContextTransaction) => TRPCContext + createExpressContext: (context: CreateExpressContextOptions) => TRPCContext + createWebSocketContext: (context: CreateWSSContextFnOptions) => TRPCContext }>() {} export module TRPCContextCreator { @@ -55,27 +55,6 @@ export module TRPCContextCreator { const mapErrors = (effect: Effect.Effect) => Effect.sandbox(effect).pipe( Effect.catchTags({ - Die: cause => Effect.fail( - new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - cause: new TRCPErrorCause(cause), - }) - ), - - Interrupt: cause => Effect.fail( - new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - cause: new TRCPErrorCause(cause), - }) - ), - - Fail: cause => Effect.fail( - new TRPCError({ - code: "INTERNAL_SERVER_ERROR", - cause: new TRCPErrorCause(cause), - }) - ), - Empty: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", @@ -83,7 +62,25 @@ const mapErrors = (effect: Effect.Effect) => }) ), - Parallel: cause => Effect.fail( + Fail: cause => Effect.fail( + cause.error instanceof TRPCError + ? cause.error + : new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + cause: new TRCPErrorCause(cause), + }) + ), + + Die: cause => Effect.fail( + cause.defect instanceof TRPCError + ? cause.defect + : new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + cause: new TRCPErrorCause(cause), + }) + ), + + Interrupt: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause: new TRCPErrorCause(cause), @@ -96,12 +93,19 @@ const mapErrors = (effect: Effect.Effect) => cause: new TRCPErrorCause(cause), }) ), + + Parallel: cause => Effect.fail( + new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + cause: new TRCPErrorCause(cause), + }) + ), }) ) -export class TRCPErrorCause extends Error { - constructor(readonly cause: unknown) { +export class TRCPErrorCause extends Error { + constructor(readonly cause: Cause.Cause) { super() } }