Skip to content

Commit 529ca15

Browse files
refactor: remove TRPCError handling from getServerErrorFromUnknown (calcom#25691)
* refactor: remove TRPCError handling from getServerErrorFromUnknown This removes the @trpc/server dependency from packages/lib by removing TRPCError handling from getServerErrorFromUnknown.ts. packages/lib is meant to be a general utility library that other packages depend on, so it shouldn't import tRPC. The callers that need to handle TRPCError (like onErrorHandler.ts and defaultResponder.ts) already handle it explicitly before calling getServerErrorFromUnknown, so this change has no behavioral impact. Changes: - Remove TRPCError and getHTTPStatusCodeFromError imports - Remove TRPCError instanceof check from getServerErrorFromUnknown - Update docstring to note that TRPCError is not handled - Remove TRPCError-related tests (they belong in the tRPC layer) - Fix lint warnings in Prisma error tests (replace 'as any' with proper type) Co-Authored-By: benny@cal.com <sldisek783@gmail.com> * wip --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent be12ac7 commit 529ca15

3 files changed

Lines changed: 28 additions & 52 deletions

File tree

packages/lib/eslint.config.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,27 @@ export default [
1313
target: ".",
1414
message: "lib package should not import from features to avoid circular dependencies.",
1515
}),
16+
forbid({
17+
from: "../trpc/**",
18+
target: ".",
19+
message: "lib package should not import from trpc to avoid circular dependencies.",
20+
}),
21+
{
22+
// Block @trpc/server imports to keep packages/lib framework-agnostic.
23+
// defaultResponder.ts is temporarily excluded until it can be refactored.
24+
ignores: ["server/defaultResponder.ts", "server/defaultResponder.test.ts"],
25+
rules: {
26+
"no-restricted-imports": [
27+
"error",
28+
{
29+
paths: [
30+
{
31+
name: "@trpc/server",
32+
message: "packages/lib should not import from @trpc/server to keep it framework-agnostic.",
33+
},
34+
],
35+
},
36+
],
37+
},
38+
},
1639
];

packages/lib/server/getServerErrorFromUnknown.test.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { Prisma } from "@calcom/prisma/client";
21
import { describe, expect, test } from "vitest";
32
import { ZodError } from "zod";
43

54
import { ErrorCode } from "@calcom/lib/errorCodes";
65
import { ErrorWithCode } from "@calcom/lib/errors";
7-
8-
import { TRPCError } from "@trpc/server";
6+
import { Prisma } from "@calcom/prisma/client";
97

108
import { HttpError } from "../http-error";
119
import { TracedError } from "../tracing/error";
@@ -156,42 +154,6 @@ describe("TracedError handling", () => {
156154
});
157155
});
158156

159-
describe("TRPCError handling", () => {
160-
test("should handle TRPCError with BAD_REQUEST", () => {
161-
const trpcError = new TRPCError({
162-
code: "BAD_REQUEST",
163-
message: "Invalid input data",
164-
});
165-
166-
const result = getServerErrorFromUnknown(trpcError);
167-
168-
expect(result.statusCode).toBe(400);
169-
expect(result.message).toBe("Invalid input data");
170-
expect(result.data).toBeUndefined();
171-
});
172-
173-
test("should handle TracedError wrapping TRPCError", () => {
174-
const trpcError = new TRPCError({
175-
code: "NOT_FOUND",
176-
message: "Resource not found",
177-
});
178-
const tracedData = { resourceId: "789" };
179-
const traceContext = {
180-
traceId: "trace_trpc123",
181-
spanId: "span_trpc123",
182-
operation: "resource_lookup",
183-
};
184-
185-
const tracedError = new TracedError(trpcError, traceContext, tracedData);
186-
187-
const result = getServerErrorFromUnknown(tracedError);
188-
189-
expect(result.statusCode).toBe(404);
190-
expect(result.message).toBe("Resource not found");
191-
expect(result.data).toEqual({ ...tracedData, traceId: traceContext.traceId });
192-
});
193-
});
194-
195157
describe("ZodError handling", () => {
196158
test("should handle ZodError with validation issues", () => {
197159
const zodError = new ZodError([

packages/lib/server/getServerErrorFromUnknown.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import { ErrorCode } from "@calcom/lib/errorCodes";
55
import { ErrorWithCode } from "@calcom/lib/errors";
66
import { Prisma } from "@calcom/prisma/client";
77

8-
import { TRPCError } from "@trpc/server";
9-
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
10-
118
import { HttpError } from "../http-error";
129
import { redactError } from "../redactError";
1310
import { stripeInvalidRequestErrorSchema } from "../stripe-error";
@@ -40,8 +37,11 @@ function parseZodErrorIssues(issues: ZodIssue[]): string {
4037
/**
4138
* Converts unknown error types to HttpError with proper status code mapping and error redaction.
4239
* SERVER-ONLY: This function imports Prisma and Stripe schemas and should only be used in server-side code.
43-
* Use in API routes, tRPC handlers, webhooks, and server-side services.
40+
* Use in API routes, webhooks, and server-side services.
4441
* For client-side code, use getErrorFromUnknown from @calcom/lib/errors instead.
42+
*
43+
* NOTE: This function does NOT handle TRPCError. Callers that need to handle TRPCError should do so
44+
* explicitly before calling this function (see onErrorHandler.ts and defaultResponder.ts for examples).
4545
*/
4646
export function getServerErrorFromUnknown(cause: unknown): HttpError {
4747
let traceId: string | undefined;
@@ -53,15 +53,6 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
5353
cause = cause.originalError;
5454
}
5555

56-
if (cause instanceof TRPCError) {
57-
const statusCode = getHTTPStatusCodeFromError(cause);
58-
return new HttpError({
59-
statusCode,
60-
message: cause.message,
61-
cause,
62-
data: traceId ? { ...tracedData, traceId } : undefined,
63-
});
64-
}
6556
if (isZodError(cause)) {
6657
return new HttpError({
6758
statusCode: 400,

0 commit comments

Comments
 (0)