Skip to content

Commit 1079569

Browse files
committed
chore: reuse client error codes
1 parent 05094ae commit 1079569

2 files changed

Lines changed: 19 additions & 32 deletions

File tree

src/app/api/internal/trpc/[trpc]/route.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {createTrpcContext} from '@/trpc/init';
1+
import {CLIENT_ERROR_CODES, createTrpcContext} from '@/trpc/init';
22
import {appRouter} from '@/trpc/routers/_app';
33
import {fetchRequestHandler} from '@trpc/server/adapters/fetch';
44

@@ -7,23 +7,6 @@ export const revalidate = 0;
77
export const fetchCache = 'force-no-store';
88
export const runtime = 'nodejs';
99

10-
// Client errors that shouldn't be logged at error level
11-
const CLIENT_ERROR_CODES = new Set([
12-
'PARSE_ERROR',
13-
'BAD_REQUEST',
14-
'UNAUTHORIZED',
15-
'NOT_FOUND',
16-
'FORBIDDEN',
17-
'METHOD_NOT_SUPPORTED',
18-
'TIMEOUT',
19-
'CONFLICT',
20-
'PRECONDITION_FAILED',
21-
'PAYLOAD_TOO_LARGE',
22-
'UNPROCESSABLE_CONTENT',
23-
'TOO_MANY_REQUESTS',
24-
'CLIENT_CLOSED_REQUEST',
25-
]);
26-
2710
const handler = async (req: Request) => {
2811
return fetchRequestHandler({
2912
endpoint: '/api/internal/trpc',

src/trpc/init.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ export const createTrpcContext = cache(async (): Promise<TrpcContext> => {
5050
};
5151
});
5252

53+
// Client errors that shouldn't be logged at error level
54+
export const CLIENT_ERROR_CODES = new Set([
55+
'PARSE_ERROR',
56+
'BAD_REQUEST',
57+
'UNAUTHORIZED',
58+
'NOT_FOUND',
59+
'FORBIDDEN',
60+
'METHOD_NOT_SUPPORTED',
61+
'TIMEOUT',
62+
'CONFLICT',
63+
'PRECONDITION_FAILED',
64+
'PAYLOAD_TOO_LARGE',
65+
'UNPROCESSABLE_CONTENT',
66+
'TOO_MANY_REQUESTS',
67+
'CLIENT_CLOSED_REQUEST',
68+
]);
69+
5370
// Avoid exporting the entire t-object
5471
// since it's not very descriptive.
5572
// For instance, the use of a t variable
@@ -61,20 +78,7 @@ const t = initTRPC.context<TrpcContext>().create({
6178
transformer: superjson,
6279
errorFormatter: ({shape, error, path}) => {
6380
// Only capture server errors to Sentry, not client errors (crawlers, bad requests, etc.)
64-
const isClientError =
65-
error.code === 'PARSE_ERROR' ||
66-
error.code === 'BAD_REQUEST' ||
67-
error.code === 'UNAUTHORIZED' ||
68-
error.code === 'NOT_FOUND' ||
69-
error.code === 'FORBIDDEN' ||
70-
error.code === 'METHOD_NOT_SUPPORTED' ||
71-
error.code === 'TIMEOUT' ||
72-
error.code === 'CONFLICT' ||
73-
error.code === 'PRECONDITION_FAILED' ||
74-
error.code === 'PAYLOAD_TOO_LARGE' ||
75-
error.code === 'UNPROCESSABLE_CONTENT' ||
76-
error.code === 'TOO_MANY_REQUESTS' ||
77-
error.code === 'CLIENT_CLOSED_REQUEST';
81+
const isClientError = CLIENT_ERROR_CODES.has(error.code);
7882

7983
if (!isClientError) {
8084
Sentry.captureException(error.cause ?? error, {

0 commit comments

Comments
 (0)