sentry-javascript: [@sentry/nextjs] Errored API routes trigger "API resolved without sending a response"
- 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.10.0
Description
Describe your issue in detail, ideally, you have a reproducible demo that you can show.
The withSentry() API route wrapper monkeypatches res.end() function to await for flush before closing a request. While this is certainly useful for serverless environments, it could cause a false positive warning on deployments with a custom server (e.g., Express).
Since normal Next API route code does not await for res.end() to finish, the handler’s promise returns before the response has ended properly. This causes Next to detect the API route as having finished without a response, even though the response is sent a few moments later. Here is an example:

A workaround is to wrap all API routes with another callback that always performs await res.end() before returning.
About this issue
- Original URL
 - State: closed
 - Created 3 years ago
 - Reactions: 23
 - Comments: 32 (5 by maintainers)
 
Commits related to this issue
- Workaround for sentry/nextjs async end per #3852 — committed to etrepum/sentry-javascript by etrepum 3 years ago
 - fix(nextjs): Prevent false `API resolved without sending a response` warning (#4139) This prevents a false positive warning from nextjs which arises from the interaction of our API route wrapper `wit... — committed to getsentry/sentry-javascript by lobsterkatie 3 years ago
 - fix(nextjs): Revert #4139 - remove manipulation of `res.finished` value (#4516) In https://github.com/getsentry/sentry-javascript/pull/4139, a change was introduced in order to suppress a false posit... — committed to getsentry/sentry-javascript by lobsterkatie 2 years ago
 - fix(nextjs): Add env var to suppress API non-response meta-warning (#4706) In the nextjs SDK, when we wrap users' API routes, we also wrap the response's `end` method, in order to keep the lambda run... — committed to getsentry/sentry-javascript by lobsterkatie 2 years ago
 - fix: set config.api.externalResolver to true in order to avoid weird behavior (https://github.com/getsentry/sentry-javascript/issues/3852#issuecomment-918820923) — committed to kamilkisiela/graphql-hive by n1ru4l 2 years ago
 - fix: set config.api.externalResolver to true in order to avoid weird … (#336) * fix: set config.api.externalResolver to true in order to avoid weird behavior (https://github.com/getsentry/sentry-java... — committed to kamilkisiela/graphql-hive by n1ru4l 2 years ago
 - feat(nextjs): Auto-wrap API routes (#5778) As part of https://github.com/getsentry/sentry-javascript/issues/5505, this applies to API route handlers the same kind of auto-wrapping we've done with the... — committed to getsentry/sentry-javascript by lobsterkatie 2 years ago
 
Please do not close
Please don’t close - this is still an issue that should be resolved
I’m still seeing the messages on
7.24.2, I have removed all of mywithSentrywrappers and I never added theexternalResolverchange.Why is this error closed? With the last version I still see the error, there is a warning but the problem is still there.
Any plan on this one? I’m currently doing something like this to get rid of the message
You can set
externalResolvertotruein api route config since it is handled by sentry (external) https://nextjs.org/docs/api-routes/api-middlewares#custom-configWe ever fixing this or what? Annoying to have logs spammed.
It does this for all my requests as well, even though I do this:
return res.status(200).json({ success: true });I hope this will be fixed 😃
I can confirm this is an issue on
7.32.1but the same pattern was working fine on7.31.1.I didn’t see anything in the diff which would indicate the breaking change, but the constant logging has definitely returned. Downgrading to
7.31.1resolves again.Resolved by https://github.com/getsentry/sentry-javascript/pull/5778, which is published with 7.14.0.
It seems like the
SENTRY_IGNORE_API_RESOLUTION_ERRORenvironment variable is not being respected anymore either. This is especially ironic since we also added theexternalResolverAPI prop on every handler, so we get no logging from NextJS but we get Sentry’s warning that NextJS might log for every request. Is that variable still working for anyone?Edit: I don’t even see anything in the repo that’d be looking for such a variable, but maybe it’s buried in the CLI itself? In my case it’s being provided in
next.config.jsalongside other env variables such asSENTRY_URL,SENTRY_PROJECT, etc, and all of those are working fine.Because it was fixed… in a way which turned out to break other things, and had to be reverted. I’ll reopen.
As for finding a new solution, I’m open to ideas. We’ve been focusing on trying to ship our next major version, so I haven’t been able to dive back into this at a deep level, but I will admit that even coming up with my turned-out-to-cause-other-problems hack took quite a bit of poking and prodding and digging through not only nextjs source code but node source code as well, and so I don’t expect finding a new approach is going to be a trivial problem.
In the meantime, you can suppress next’s warning yourself by using the “external resolver” option in your API route (docs here). I’ve also just pushed https://github.com/getsentry/sentry-javascript/pull/4706, to allow our warning to be suppressed as well.
@kaykdm Thanks for this workaround! Is there a way to set a default config for all API routes ? From the docs it seems that there is no option to do this in
next.config.js, which means this config has to be added to all API routes individually.@KieraDOG the workaround in this comment did not work for me. This resulted in the response being ended before the actual content is sent, which means stalling requests (especially in a development environment).