-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 500 error replaced to 400 & sended to sentry
- Loading branch information
1 parent
a4d2cb9
commit 5a879ed
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}, | ||
}); |