Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore(trpc): resolve trpc errors from instances of BaseErrors (#9968)

marliessophiecb84563b06d4
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 5a55873647..6cc1be9bbc 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -22,6 +22,7 @@ import { prisma, Role } from "@langfuse/shared/src/db";
import * as z from "zod/v4";
import * as opentelemetry from "@opentelemetry/api";
import { type IncomingHttpHeaders } from "node:http";
+import { getTRPCErrorCodeFromHTTPStatusCode } from "@/src/server/utils/trpc-utils";
type CreateContextOptions = {
session: Session | null;
@@ -93,6 +94,7 @@ import {
import { AdminApiAuthService } from "@/src/ee/features/admin-api/server/adminApiAuth";
import { env } from "@/src/env.mjs";
+import { BaseError } from "@langfuse/shared";
setUpSuperjson();
@@ -126,6 +128,16 @@ const t = initTRPC.context<typeof createTRPCContext>().create({
*/
export const createTRPCRouter = t.router;
+const resolveError = (error: TRPCError) => {
+ if (error.cause instanceof BaseError) {
+ return {
+ code: getTRPCErrorCodeFromHTTPStatusCode(error.cause.httpCode),
+ httpStatus: error.cause.httpCode,
+ };
+ }
+ return { code: error.code, httpStatus: getHTTPStatusCodeFromError(error) };
+};
+
// global error handling
const withErrorHandling = t.middleware(async ({ ctx, next }) => {
const res = await next({ ctx }); // pass the context to the next middleware
@@ -154,14 +166,14 @@ const withErrorHandling = t.middleware(async ({ ctx, next }) => {
// Throw a new TRPC error with:
// - The same error code as the original error
// - Either the original error message OR "Internal error" if it's a 5xx error
- const httpStatus = getHTTPStatusCodeFromError(res.error);
+ const { code, httpStatus } = resolveError(res.error);
const isSafeToExpose = httpStatus >= 400 && httpStatus < 500;
const errorMessage = isLangfuseCloud
? "We have been notified and are working on it."
: "Please check error logs in your self-hosted deployment.";
res.error = new TRPCError({
- code: res.error.code,
+ code,
cause: null, // do not expose stack traces
message: isSafeToExpose
? res.error.message
diff --git a/web/src/server/utils/trpc-utils.ts b/web/src/server/utils/trpc-utils.ts
new file mode 100644
index 0000000000..0f99c58ddc
--- /dev/null
+++ b/web/src/server/utils/trpc-utils.ts
@@ -0,0 +1,33 @@
+import type { TRPC_ERROR_CODE_KEY } from "@trpc/server";
+
+// Note: copied from official documentation: https://trpc.io/docs/server/error-handling#error-codes
+const HTTP_STATUS_CODE_TO_TRPC_ERROR_CODE: Record<number, TRPC_ERROR_CODE_KEY> =
+ {
+ 400: "BAD_REQUEST",
+ 401: "UNAUTHORIZED",
+ 402: "PAYMENT_REQUIRED",
+ 403: "FORBIDDEN",
+ 404: "NOT_FOUND",
+ 405: "METHOD_NOT_SUPPORTED",
+ 408: "TIMEOUT",
+ 409: "CONFLICT",
+ 412: "PRECONDITION_FAILED",
+ 413: "PAYLOAD_TOO_LARGE",
+ 415: "UNSUPPORTED_MEDIA_TYPE",
+ 422: "UNPROCESSABLE_CONTENT",
+ 429: "TOO_MANY_REQUESTS",
+ 499: "CLIENT_CLOSED_REQUEST",
+ 500: "INTERNAL_SERVER_ERROR",
+ 501: "NOT_IMPLEMENTED",
+ 502: "BAD_GATEWAY",
+ 503: "SERVICE_UNAVAILABLE",
+ 504: "GATEWAY_TIMEOUT",
+ };
+
+const DEFAULT_ERROR_CODE: TRPC_ERROR_CODE_KEY = "INTERNAL_SERVER_ERROR";
+
+export const getTRPCErrorCodeFromHTTPStatusCode = (
+ httpStatus: number,
+): TRPC_ERROR_CODE_KEY => {
+ return HTTP_STATUS_CODE_TO_TRPC_ERROR_CODE[httpStatus] ?? DEFAULT_ERROR_CODE;
+};