sentry-javascript: @sentry/nextjs not reporting errors from API (serverless) endpoints in Vercel
- Review the documentation: https://docs.sentry.io/
- Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
- Use the latest release: https://github.com/getsentry/sentry-javascript/releases
- Provide a link to the affected event from your Sentry account
Package + Version
-
@sentry/nextjs
Version:
6.11.0
Description
I was unable to get error reporting from API endpoints deployed in Vercel (as serverless functions) with @sentry/nextjs version 6.11.0 but downgrading to 6.4.1 fixed in.
Our app is a new one built with Next.js 11.1 from create-next-app as of last week.
We used the latest @sentry/nextjs configured according to the documentation, and with additional changes following the with-sentry example: specifically, added _error.js page and err pass through to <Component/> in _app.js.
Before downgrading, client errors were logged fine but I couldn’t get error reports from Vercel serverless functions at all.
Both client- and server-side errors were logged successfully to Sentry when running the project locally with yarn dev.
This is the failing API endpoint I used to test in file pages/api/boom.js:
import { withSentry } from "@sentry/nextjs";
const handler = async function handler(req, res) {
// Trigger an exception
boom();
res.status(200).send("OK");
};
export default withSentry(handler);
The problem seems similar to other reported issues:
- https://github.com/getsentry/sentry-javascript/issues/3643 (closed at time of writing, otherwise I would have commented there instead)
- https://github.com/getsentry/sentry-javascript/issues/3869 (open at time of writing; seemingly fixed, but does not mention API routes)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 28 (8 by maintainers)
Commits related to this issue
- Ensure `withSentry` reports API (serverless) errors from Vercel Potential fix for #3917 to ensure Sentry error reporting clean-up is done -- transaction finished and error flushed -- when run from a ... — committed to museumofoldandnewart/sentry-javascript by jmurty 3 years ago
- fix(nextjs): Delay error propagation until `withSentry` is done (#4027) In our nextjs API route wrapper `withSentry`, we capture any errors thrown by the original handler, and then once we've capture... — committed to getsentry/sentry-javascript by lobsterkatie 3 years ago
- fix(nextjs): Delay error propagation until `withSentry` is done (#4027) In our nextjs API route wrapper `withSentry`, we capture any errors thrown by the original handler, and then once we've capture... — committed to getsentry/sentry-javascript by lobsterkatie 3 years ago
- fix(nextjs): Delay error propagation until `withSentry` is done (#4027) In our nextjs API route wrapper `withSentry`, we capture any errors thrown by the original handler, and then once we've capture... — committed to getsentry/sentry-javascript by lobsterkatie 3 years ago
@jmurty @DavidChouinard Thanks to both of you! One of my colleagues has been looking at this while I’ve been pulled away on another project, and I’m now going to poke at it, too. Will let you know what I come up with!
Hi @lobsterkatie I have set up a minimal example Next.js + Sentry project to demonstrate the issue: https://github.com/jmurty/nextjs-sentry-vercel-test
This project is set up using the Next.js and Sentry installation wizards, with some extra steps to work better with Vercel. Hopefully the commit messages explain what I did.
I also set up a new Sentry project to log messages to: https://sentry.io/organizations/murty/issues/?project=5925157 (org
murtyprojectnextjs-sentry-vercel-test)When run the app locally with
yarn dev(or withyarn startafteryarn build) clicking the button on the index page or visiting the API endpoints /api/test1 through /api/test4 trigger an error that gets logged to the Sentry project.The same app is deployed to Vercel at https://nextjs-sentry-vercel-test.vercel.app/ But when I trigger the errors on the Vercel site only the client-side (button press) and /api/test4 errors get logged. Errors are not logged for /api/test1, /api/test2, or /api/test3
The /api/test4 API endpoint explicitly catches and captures the error to Sentry.
The /api/test3 is closest to this issue: an error raised in the Next.js handler function that should be caught and captured by
withSentry.The other two examples trigger errors outside the scope of the the handler function and
withSentryso should presumably be caught and captured by the custom _error.js page as a fallback. The custom error page is displayed for these endpoints.Here are the logs from Vercel after hitting the four error-triggering API endpoints: nextjs-sentry-vercel-test.vercel.logs.txt
Having the same issue.
Looks like
withSentryis calling synchronous function captureException and throws right afterwards before events are sent to the sentry server. https://github.com/getsentry/sentry-javascript/blob/952850f9845f8fc58f306049063ba6acdae6cfb7/packages/nextjs/src/utils/withSentry.ts#L76-L90Probably it should be changed to something like this:
This fix probably explains why the workaround mentioned above is working.
I also created a
sentry-6.4.1branch on the example project where I downgraded@sentry/nextjsfrom 6.11 to 6.4.1: https://github.com/jmurty/nextjs-sentry-vercel-test/tree/sentry-6.4.1This branch is running on Vercel at https://nextjs-sentry-vercel-test-git-sentry-641-jmurty.vercel.app/ and with it the /api/test3 trigger is logged unlike with version 6.11. This matches my experience where downgrading fixed logging of errors within the handler function.
The client-side and /api/test4 triggers are also logged, like the 6.11 version. The /api/test1 and /api/test2 example errors are not logged.
Here are the Vercel logs again for the 6.4.1 deployment: nextjs-sentry-vercel-test.6_4_1.vercel.logs.txt
@lobsterkatie any update on this?
I also have the issue. Does anyone know how to solve this? Here us my sentry configuration for the cloud functions: https://github.com/langfuse/langfuse/blob/main/sentry.server.config.ts
I have re-tested my example repo after upgrading to
@sentry/nextjsversion 6.13.2 and the issue remains, specifically the/api/test3endpoint does not log errors to Sentry from Vercel.Following @anguskeatinge’s suggestion I added a
customWithSentryon this branch https://github.com/jmurty/nextjs-sentry-vercel-test/tree/ak-workaround and deployed to Vercel at https://nextjs-sentry-vercel-test-git-ak-workaround-jmurty.vercel.app/Using the very basic custom
withSentryalternative theapi/test3endpoint does log an error to Sentry from Vercel.