rsnext/examples/with-sentry/pages/_error.js

31 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
* This page is loaded by Nextjs:
* - on the server, when data-fetching methods throw or reject
* - on the client, when `getInitialProps` throws or rejects
* - on the client, when a React lifecycle method throws or rejects, and it's
* caught by the built-in Nextjs error boundary
*
* See:
* - https://nextjs.org/docs/basic-features/data-fetching/overview
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
* - https://reactjs.org/docs/error-boundaries.html
*/
import * as Sentry from '@sentry/nextjs'
import NextErrorComponent from 'next/error'
const CustomErrorComponent = (props) => (
<NextErrorComponent statusCode={props.statusCode} />
)
Update with-sentry example to show how to track API routes + bug fixes (#16484) ## New features - Example of how to use `Sentry.captureException()` in API routes https://github.com/vercel/next.js/commit/5a35982717bc7f916022dd713b3b1f9e0921d503 ## Bug fixes - Server-side source maps now work in Sentry when deployed to Vercel https://github.com/vercel/next.js/commit/629a9ed5048629dc12670ef457fe6efaa2e8d3b6 - If uploading source maps, `Sentry.init()` now sets the `release`, so that exceptions are correctly associated with the source maps in that release ## Open issues ### `_error.js` is unused for `getServerSideProps` errors when deployed to Vercel Instead of rendering this example's overridden `_error.js` (like it does when testing locally with `NODE_ENV='production'`), when deployed to Vercel and an Error is thrown from `getServerSideProps`, this page is shown: ![Screenshot of Vercel Application Error page](https://user-images.githubusercontent.com/709153/90968889-aa3c3d00-e4a6-11ea-9eff-fafee3d1ff33.png) This confuses me because the Error is still successfully sent to Sentry, but I would've expected it to be sent by the call to `Sentry.captureException()` in `_error.js`. I'm not sure why it works. ### API test 2 & 3 don't work in the "server" build config They don't work when deployed to Vercel in the "serverless" config either, but that's expected because there aren't any handlers to flush the Sentry queue. I can't figure out why they don't work in the long-lived "server" config.
2020-11-09 19:36:36 +01:00
CustomErrorComponent.getInitialProps = async (contextData) => {
// In case this is running in a serverless function, await this in order to give Sentry
// time to send the error before the lambda exits
await Sentry.captureUnderscoreErrorException(contextData)
// This will contain the status code of the response
return NextErrorComponent.getInitialProps(contextData)
}
export default CustomErrorComponent