From a0382a2427628975977938c3d6a7c091c15e9034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Valverd=C3=A9?= Date: Fri, 12 Jul 2024 03:53:04 +0200 Subject: [PATCH] mapErrorsToTRPC --- packages/server/src/trpc/TRPCContext.ts | 11 +++++-- .../server/src/trpc/TRPCContextCreator.ts | 29 +++++++++++++++++-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/server/src/trpc/TRPCContext.ts b/packages/server/src/trpc/TRPCContext.ts index 51d7534..3f40cf5 100644 --- a/packages/server/src/trpc/TRPCContext.ts +++ b/packages/server/src/trpc/TRPCContext.ts @@ -1,14 +1,21 @@ +import type { TRPCError } from "@trpc/server" import type { Effect, Runtime } from "effect" -import type { Request } from "express" +import type { RuntimeFiber } from "effect/Fiber" import type { Services } from "../Services" export interface TRPCContext { runtime: Runtime.Runtime + run: ( effect: Effect.Effect, - options?: { readonly signal?: AbortSignal } + options?: { readonly signal?: AbortSignal }, ) => Promise + fork: ( + effect: Effect.Effect, + options?: Runtime.RunForkOptions, + ) => RuntimeFiber + // req: Request } diff --git a/packages/server/src/trpc/TRPCContextCreator.ts b/packages/server/src/trpc/TRPCContextCreator.ts index 776d4c5..9b2e0ee 100644 --- a/packages/server/src/trpc/TRPCContextCreator.ts +++ b/packages/server/src/trpc/TRPCContextCreator.ts @@ -19,11 +19,27 @@ export class TRPCContextCreator extends Context.Tag("TRPCContextCreator")() - const run = Runtime.runPromise(runtime) + + const run = ( + effect: Effect.Effect, + options?: { readonly signal?: AbortSignal }, + ) => Runtime.runPromise(runtime)( + effect.pipe(mapErrorsToTRPC), + options, + ) + + const fork = ( + effect: Effect.Effect, + options?: Runtime.RunForkOptions, + ) => Runtime.runFork(runtime)( + effect.pipe(mapErrorsToTRPC), + options, + ) return ({ req }) => ({ runtime, run, + fork, // req, }) })) @@ -36,13 +52,20 @@ const mapErrorsToTRPC = (effect: Effect.Effect) => Die: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), - Interrupt: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), - Fail: cause => Effect.fail( new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) ), + Empty: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) + ), + Parallel: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) + ), + Sequential: cause => Effect.fail( + new TRPCError({ code: "INTERNAL_SERVER_ERROR", cause }) + ), }) )