Skip to content

Instantly share code, notes, and snippets.

@mcdougal
Created September 28, 2018 11:53
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcdougal/7bf001417c3dc4b579da224b12776691 to your computer and use it in GitHub Desktop.
Save mcdougal/7bf001417c3dc4b579da224b12776691 to your computer and use it in GitHub Desktop.
Using @sentry/browser with Next.js for client and server-side rendering
import * as Sentry from '@sentry/browser';
import getConfig from 'next/config';
import React from 'react';
const { SENTRY_DSN } = getConfig().publicRuntimeConfig;
Sentry.init({ dsn: SENTRY_DSN });
/**
* Send an error event to Sentry.
*
* Server-side:
* Next.js captures SSR errors and never passes them to the Sentry
* middleware. It does, however, render the _error.js component, so
* we can manually fire Sentry events here.
*
* Client-side:
* Unhandled client exceptions will always bubble up to the _error.js
* component, so we can manually fire events here.
*/
const notifySentry = (err, req, statusCode) => {
Sentry.configureScope((scope) => {
if (!req) {
scope.setTag(`ssr`, false);
} else {
scope.setTag(`ssr`, true);
scope.setExtra(`url`, req.url);
scope.setExtra(`params`, req.params);
scope.setExtra(`query`, req.query);
scope.setExtra(`statusCode`, statusCode);
scope.setExtra(`headers`, req.headers);
if (req.user) {
scope.setUser({ id: req.user.id, email: req.user.email });
}
}
});
Sentry.captureException(err);
};
export default class Error extends React.Component {
static async getInitialProps({ req, res, err }) {
let statusCode;
if (res) {
({ statusCode } = res);
} else if (err) {
({ statusCode } = err);
} else {
statusCode = null;
}
notifySentry(err, req, statusCode);
return { statusCode };
}
render() {
const { statusCode } = this.props;
return statusCode;
}
}
@Enalmada
Copy link

Thank you so much for posting this. It is critical to have sentry logging all the errors client and server side and this seems like the best way to do it for now.

@revolunet
Copy link

thanks for sharing. how can node use the @sentry/browser module ?

@cjayyy
Copy link

cjayyy commented Mar 13, 2019

Thanks for sharing! ❤️

@Janealter
Copy link

Janealter commented Apr 23, 2019

Not working. Error Sentry Logger [Error]: Error while sending event: ReferenceError: XMLHttpRequest is not defined. What server.js looks like?

@leerob
Copy link

leerob commented Apr 23, 2019

I've been doing a bit of digging to get Sentry configured correctly before shipping some of our apps to production. I wrote down what it takes to get a simple Sentry setup going here. Hope this helps!

@Janealter
Copy link

import * as Sentry from '@sentry/browser' imports client-side version of Sentry. But when I try to call captureException on the server side, I get the error I wrote about above. This is due to the fact that NodeJS does not have an XMLHTTPRequest. How does it work for you?

@corysimmons
Copy link

@leerob

There are still a few issues with it, so I wouldn't recommend using it as a base for now. You might be able to extract pieces of the complex example and apply them as you see fit.

Thanks for sharing, but please put that at the top of your doc.

For everyone else here trying to get this working, check out https://github.com/zeit/next.js/blob/canary/examples/with-sentry <-- official example.

@leerob
Copy link

leerob commented Jun 12, 2019

@corysimmons That quote is talking about the with-sentry example - not the code I provided.

That was the primary reason for me researching an easier way to set up Sentry. The "official" example was a little too complex. Hopefully that helps clarify 👍

@corysimmons
Copy link

Oh! I misunderstood what it was referring to in There are still a few issues with it. In that case your simple example will be really helpful (the complex one didn't even work in the end.. rip).

@a14m
Copy link

a14m commented Apr 3, 2020

@leerob, could you explain more the part with uploading the source map to sentry (and not providing them publicly)...
I've followed the examples but cannot find a way to properly show the error stack in sentry
(full reference: https://stackoverflow.com/questions/61011281/next-js-source-maps-with-typescript-on-sentry)

@ShreyKumar
Copy link

@leerob, I've tried your example, but still doesn't seem to report errors on time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment