Skip to content

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

chore: global error handling for `trpc` routes (#3959)

marliessophie10d2299fae11
diff --git a/web/src/features/notifications/ErrorNotification.tsx b/web/src/features/notifications/ErrorNotification.tsx
index 14a0bff6d3..360c04a0a8 100644
--- a/web/src/features/notifications/ErrorNotification.tsx
+++ b/web/src/features/notifications/ErrorNotification.tsx
@@ -9,7 +9,6 @@ interface ErrorNotificationProps {
error: string;
description: string;
type: "WARNING" | "ERROR";
- cause?: string;
dismissToast: (t?: string | number | undefined) => void;
toast: string | number;
path?: string;
@@ -19,7 +18,6 @@ export const ErrorNotification: React.FC<ErrorNotificationProps> = ({
error,
description,
type,
- cause,
dismissToast,
toast,
path,
@@ -32,7 +30,7 @@ export const ErrorNotification: React.FC<ErrorNotificationProps> = ({
const handleReportIssueClick = () => {
if (chatAvailable) {
const currentUrl = window.location.href;
- const message = `I received the following error:\n\nError: ${error}\nDescription: ${description}\n ${cause ? `Cause: ${cause}\n` : ""}${path ? `Path: ${path}\n` : ""}URL: ${currentUrl}`;
+ const message = `I received the following error:\n\nError: ${error}\nDescription: ${description}\n ${path ? `Path: ${path}\n` : ""}URL: ${currentUrl}`;
sendUserChatMessage(message);
dismissToast(toast);
}
@@ -57,13 +55,7 @@ export const ErrorNotification: React.FC<ErrorNotificationProps> = ({
Path: {path}
</div>
)}
- {cause && (
- <div
- className={`max-h-32 overflow-y-auto text-sm leading-tight ${textColor}`}
- >
- {cause}
- </div>
- )}
+
{isError && chatAvailable && (
<Button
variant="errorNotification"
diff --git a/web/src/features/notifications/showErrorToast.tsx b/web/src/features/notifications/showErrorToast.tsx
index eeacf21bae..34f58074fd 100644
--- a/web/src/features/notifications/showErrorToast.tsx
+++ b/web/src/features/notifications/showErrorToast.tsx
@@ -15,7 +15,6 @@ export const showErrorToast = (
error: string,
description: string,
type: "WARNING" | "ERROR" = "ERROR",
- cause?: string,
path?: string,
) => {
toast.custom(
@@ -24,7 +23,6 @@ export const showErrorToast = (
error={error}
description={description}
type={type}
- cause={cause}
path={path}
dismissToast={toast.dismiss}
toast={t}
diff --git a/web/src/server/api/trpc.ts b/web/src/server/api/trpc.ts
index 630afcc9c4..51aa878621 100644
--- a/web/src/server/api/trpc.ts
+++ b/web/src/server/api/trpc.ts
@@ -78,7 +78,7 @@ import superjson from "superjson";
import { ZodError } from "zod";
import { setUpSuperjson } from "@/src/utils/superjson";
import { DB } from "@/src/server/db";
-import { addUserToSpan } from "@langfuse/shared/src/server";
+import { addUserToSpan, logger } from "@langfuse/shared/src/server";
setUpSuperjson();
@@ -110,6 +110,28 @@ const t = initTRPC.context<typeof createTRPCContext>().create({
*/
export const createTRPCRouter = t.router;
+// global error handling
+const withErrorHandling = t.middleware(async ({ ctx, next }) => {
+ const res = await next({ ctx }); // pass the context to the next middleware
+
+ if (!res.ok) {
+ logger.error("middleware intercepted error", res.error);
+
+ // 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 an INTERNAL_SERVER_ERROR
+ throw new TRPCError({
+ code: res.error.code,
+ message:
+ res.error.code !== "INTERNAL_SERVER_ERROR"
+ ? res.error.message
+ : "Internal error",
+ });
+ }
+
+ return res;
+});
+
// otel setup
const withOtelTracingProcedure = t.procedure.use(
tracing({ collectInput: true, collectResult: true }),
@@ -122,7 +144,7 @@ const withOtelTracingProcedure = t.procedure.use(
* are logged in.
*/
-export const publicProcedure = withOtelTracingProcedure;
+export const publicProcedure = withOtelTracingProcedure.use(withErrorHandling);
/** Reusable middleware that enforces users are logged in before running the procedure. */
const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
@@ -145,8 +167,9 @@ const enforceUserIsAuthed = t.middleware(({ ctx, next }) => {
*
* @see https://trpc.io/docs/procedures
*/
-export const protectedProcedure =
- withOtelTracingProcedure.use(enforceUserIsAuthed);
+export const protectedProcedure = withOtelTracingProcedure
+ .use(withErrorHandling)
+ .use(enforceUserIsAuthed);
const inputProjectSchema = z.object({
projectId: z.string(),
@@ -231,9 +254,9 @@ const enforceUserIsAuthedAndProjectMember = t.middleware(
},
);
-export const protectedProjectProcedure = withOtelTracingProcedure.use(
- enforceUserIsAuthedAndProjectMember,
-);
+export const protectedProjectProcedure = withOtelTracingProcedure
+ .use(withErrorHandling)
+ .use(enforceUserIsAuthedAndProjectMember);
const inputOrganizationSchema = z.object({
orgId: z.string(),
@@ -277,9 +300,9 @@ const enforceIsAuthedAndOrgMember = t.middleware(({ ctx, rawInput, next }) => {
});
});
-export const protectedOrganizationProcedure = withOtelTracingProcedure.use(
- enforceIsAuthedAndOrgMember,
-);
+export const protectedOrganizationProcedure = withOtelTracingProcedure
+ .use(withErrorHandling)
+ .use(enforceIsAuthedAndOrgMember);
/*
* Protect trace-level getter routes.
@@ -341,8 +364,9 @@ const enforceTraceAccess = t.middleware(async ({ ctx, rawInput, next }) => {
});
});
-export const protectedGetTraceProcedure =
- withOtelTracingProcedure.use(enforceTraceAccess);
+export const protectedGetTraceProcedure = withOtelTracingProcedure
+ .use(withErrorHandling)
+ .use(enforceTraceAccess);
/*
* Protect session-level getter routes.
@@ -409,5 +433,6 @@ const enforceSessionAccess = t.middleware(async ({ ctx, rawInput, next }) => {
});
});
-export const protectedGetSessionProcedure =
- withOtelTracingProcedure.use(enforceSessionAccess);
+export const protectedGetSessionProcedure = withOtelTracingProcedure
+ .use(withErrorHandling)
+ .use(enforceSessionAccess);
diff --git a/web/src/utils/trpcErrorToast.tsx b/web/src/utils/trpcErrorToast.tsx
index b74d601fc9..ca3946008c 100644
--- a/web/src/utils/trpcErrorToast.tsx
+++ b/web/src/utils/trpcErrorToast.tsx
@@ -43,14 +43,14 @@ const getErrorTitleAndHttpCode = (error: TRPCClientError<any>) => {
return { errorTitle, httpStatus };
};
-const getErrorDescription = (message: string, httpStatus: number) => {
+const getErrorDescription = (httpStatus: number) => {
switch (httpStatus) {
case 429:
return "Rate limit hit. Please try again later.";
case 524:
return "Request took too long to process. Please try again later.";
default:
- return message;
+ return "Internal error";
}
};
@@ -59,14 +59,12 @@ export const trpcErrorToast = (error: unknown) => {
const { errorTitle, httpStatus } = getErrorTitleAndHttpCode(error);
const path = error.data?.path;
- const cause = error.data?.cause;
- const description = getErrorDescription(error.message, httpStatus);
+ const description = getErrorDescription(httpStatus);
showErrorToast(
errorTitle,
description,
httpStatus >= 500 && httpStatus < 600 ? "ERROR" : "WARNING",
- cause,
path,
);
} else {