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(examples): Use helper function in with-sentry example _error.js #37866

Conversation

lobsterkatie
Copy link
Contributor

@lobsterkatie lobsterkatie commented Jun 21, 2022

This updates the with-sentry example to use the new helper function captureUnderscoreErrorException in its _error.js page. This new function both adds more data to sentry events and allows the code to be cleaned up quite a bit.

Fixes #37870.

  • Make sure the linting passes by running pnpm lint
  • The examples guidelines are followed from our contributing doc

@ijjk ijjk added the examples Issue/PR related to examples label Jun 21, 2022
lobsterkatie added a commit to getsentry/sentry-javascript that referenced this pull request Jun 21, 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](https://github.com/getsentry/sentry-wizard/blob/v1.2.17/scripts/NextJs/configs/_error.js) 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

```js
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 the PR 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.
examples/with-sentry/pages/_error.js Show resolved Hide resolved
examples/with-sentry/pages/_error.js Outdated Show resolved Hide resolved
examples/with-sentry/pages/_error.js Outdated Show resolved Hide resolved
@lobsterkatie
Copy link
Contributor Author

lobsterkatie commented Jul 6, 2022

Chatted with @ijjk, and I'm going to wait on this until #21240 is released, as it means this PR can also remove the previous workaround for #8592. Marking as a draft in the meantime so it doesn't get merged.

@lobsterkatie lobsterkatie marked this pull request as draft July 7, 2022 01:57
@lobsterkatie lobsterkatie force-pushed the kmclb-use-helper-function-in-with-sentry-_error branch from 043f7b7 to 1a9421c Compare July 7, 2022 02:15
@lobsterkatie lobsterkatie force-pushed the kmclb-use-helper-function-in-with-sentry-_error branch 2 times, most recently from 1a9e36b to c3dd35e Compare July 15, 2022 15:19
@lobsterkatie lobsterkatie force-pushed the kmclb-use-helper-function-in-with-sentry-_error branch from c3dd35e to 71bc429 Compare July 15, 2022 18:47
@lobsterkatie lobsterkatie marked this pull request as ready for review July 15, 2022 18:48
@lobsterkatie
Copy link
Contributor Author

@ijjk this should be ready to go now!

Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kodiakhq kodiakhq bot merged commit 4a774a4 into vercel:canary Jul 15, 2022
@lobsterkatie lobsterkatie deleted the kmclb-use-helper-function-in-with-sentry-_error branch July 19, 2022 18:19
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade with-sentry example to NextJs v12 and sentry-javascript V7
4 participants