Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nextjs): Add exception handler for _error.js #5259

Merged
merged 1 commit into from
Jun 21, 2022

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Jun 14, 2022

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's with-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 and captureException 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

import * as Sentry from '@sentry/nextjs';
import NextErrorComponent from 'next/error';

const CustomErrorComponent = props => {
  Sentry.captureUnderscoreErrorException(props);
  return <NextErrorComponent statusCode={props.statusCode} />;
};

CustomErrorComponent.getInitialProps = async contextData => {
  await Sentry.captureUnderscoreErrorException(contextData);
  return NextErrorComponent.getInitialProps(contextData);
};

export default CustomErrorComponent;

(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:

  • filtering out all 400-type errors, not just 404s,
  • annotating errors with a mechanism value,
  • adding request data to the event when available, and
  • capturing a message when falsy errors are thrown.

(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:

image

After:

image

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-add-_error-handler branch 2 times, most recently from d42d517 to dea58a7 Compare June 15, 2022 03:17
@github-actions
Copy link
Contributor

github-actions bot commented Jun 15, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.35 KB (added)
@sentry/browser - ES5 CDN Bundle (minified) 59.85 KB (added)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.15 KB (added)
@sentry/browser - ES6 CDN Bundle (minified) 53.49 KB (added)
@sentry/browser - Webpack (gzipped + minified) 19.92 KB (added)
@sentry/browser - Webpack (minified) 64.85 KB (added)
@sentry/react - Webpack (gzipped + minified) 19.95 KB (added)
@sentry/nextjs Client - Webpack (gzipped + minified) 43.91 KB (added)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 25.52 KB (added)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 24.04 KB (added)

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-add-_error-handler branch 2 times, most recently from bd70aaf to 9c903e3 Compare June 15, 2022 14:44
@vladanpaunovic
Copy link
Contributor

Fixes #5131

@lobsterkatie lobsterkatie marked this pull request as ready for review June 21, 2022 06:02
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-add-_error-handler branch from 9c903e3 to 10ccd53 Compare June 21, 2022 06:03
@lobsterkatie lobsterkatie merged commit e855e76 into master Jun 21, 2022
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-add-_error-handler branch June 21, 2022 06:33
onurtemizkan pushed a commit to onurtemizkan/sentry-docs that referenced this pull request Aug 15, 2022
)

This updates the nextjs manual setup docs to use the helper function for `_error.js` introduced in getsentry/sentry-javascript#5259.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants