feat(nextjs): Add exception handler for _error.js
#5259
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In order to have Sentry capture certain kinds of errors in nextjs, users need to add a custom
_error.js
file to their projects. We do this automatically when users go through the wizard setup, but the file we've been adding is just a clone of the one in vercel'swith-sentry
nextjs example app, and is simultaneously quite verbose and pretty bare-bones in terms of what it does. (This is not a knock on the folks who wrote it, who don't have the context we do, but the fact remains that it could stand to be improved.)This does so, by creating a utility function,
captureUnderscoreErrorException
, for users to use in place of the manual edge-case handling andcaptureException
calls in the original. In addition to cleaning things up, this allows us to modify behavior, fix bugs, and add features without users having to update their code. (Existing users will have to update to use the function, of course, but after that they should never have to touch it again. And for new users, it'll be a set-it-and-forget-it.)With this change, the
_error.js
we add with the wizard becomes just(The real copy has helpful comments, but they've been removed here for the sake of brevity.)
And speaking of adding features... why not start now? This new function improves on the existing code by:
mechanism
value,(See below for screenshots of the difference adding this data makes.)
The file injected by the wizard is updated in getsentry/sentry-wizard#170, and the file used in the
with-sentry
example app is updated in vercel/next.js#37866.Ref: WEB-928
Screenshots
Before:
After: