Skip to content

Commit

Permalink
feat: 500 error replaced to 400 & sended to sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVorop committed Apr 22, 2024
1 parent a4d2cb9 commit 5a879ed
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/pages/api/trpc/[trpc].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
import * as trpcNext from '@trpc/server/adapters/next';
import * as Sentry from '@sentry/nextjs';

import { trpcRouter } from '../../../../trpc/router';
import { createContext } from '../../../../trpc/context';

const errorSubstitution = <T extends { code: string; message: string; stack?: string }>(
error: T,
code: 'BAD_REQUEST',
) => {
error.code = code;
error.message = 'Something went wrong';
delete error.stack;
};

export default trpcNext.createNextApiHandler({
router: trpcRouter,
createContext,
onError({ error, ctx, input, path, req }) {
if (error.code === 'INTERNAL_SERVER_ERROR') {
const extra = {
error: error.cause,
user: ctx?.session?.user,
input,
path,
url: req.url,
requestId: ctx?.headers['x-request-id'],
};

Sentry.captureException(error, { extra });
console.error(extra);

errorSubstitution(error, 'BAD_REQUEST');
}
},
});

0 comments on commit 5a879ed

Please sign in to comment.