Title: @sentry/nextjs not reporting errors in next.js api routes · Issue #6450 · getsentry/sentry-javascript · GitHub
Open Graph Title: @sentry/nextjs not reporting errors in next.js api routes · Issue #6450 · getsentry/sentry-javascript
X Title: @sentry/nextjs not reporting errors in next.js api routes · Issue #6450 · getsentry/sentry-javascript
Description: Version @sentry/nextjs: 7.23.0 next: 13.0.6 Issue @sentry/nextjs not reporting errors in next.js api routes Only errors from the font-end are reported to Sentry. Do not report API errors to Sentry. pages/api/test.ts import { withSentry }...
Open Graph Description: Version @sentry/nextjs: 7.23.0 next: 13.0.6 Issue @sentry/nextjs not reporting errors in next.js api routes Only errors from the font-end are reported to Sentry. Do not report API errors to Sentry....
X Description: Version @sentry/nextjs: 7.23.0 next: 13.0.6 Issue @sentry/nextjs not reporting errors in next.js api routes Only errors from the font-end are reported to Sentry. Do not report API errors to Sentry....
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/6450
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"@sentry/nextjs not reporting errors in next.js api routes","articleBody":"### Version\r\n@sentry/nextjs: 7.23.0\r\nnext: 13.0.6\r\n\r\n\r\n### Issue\r\n\r\n@sentry/nextjs not reporting errors in next.js api routes\r\nOnly errors from the font-end are reported to Sentry.\r\n\r\nDo not report API errors to Sentry.\r\n\r\n`pages/api/test.ts`\r\n```ts\r\nimport { withSentry } from \"@sentry/nextjs\"\r\nimport { OK } from \"http-status\"\r\nimport { NextApiRequest, NextApiResponse } from \"next\"\r\n\r\nconst handle = async (req: NextApiRequest, res: NextApiResponse) => {\r\n throw new Error(\"API Test 1\")\r\n\r\n return res.status(OK).end()\r\n}\r\n\r\nexport default withSentry(handle)\r\n```\r\n\r\nI completed the setting according to [the guide](https://docs.sentry.io/platforms/javascript/guides/nextjs/).\r\n\r\nBelow are the files I set up.\r\n`sentry.client.config.ts`\r\n```ts\r\nimport * as Sentry from \"@sentry/nextjs\"\r\n\r\nSentry.init({\r\n dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,\r\n tracesSampleRate: 1.0,\r\n})\r\n```\r\n\r\n`sentry.server.config.ts`\r\n```ts\r\nimport * as Sentry from \"@sentry/nextjs\"\r\n\r\nSentry.init({\r\n dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,\r\n tracesSampleRate: 1.0,\r\n debug: true,\r\n})\r\n```\r\n\r\n`next.config.js`\r\n```js\r\n/** @type {import('next').NextConfig} */\r\nconst { withSentryConfig } = require(\"@sentry/nextjs\")\r\n\r\nconst securityHeaders = [\r\n {\r\n key: \"X-XSS-Protection\",\r\n value: \"1; mode=block\",\r\n },\r\n]\r\n\r\nconst nextConfig = {\r\n reactStrictMode: false,\r\n sentry: {\r\n hideSourceMaps: true,\r\n },\r\n webpack: (config) => {\r\n if (!config.experiments) {\r\n config.experiments = {}\r\n }\r\n\r\n config.experiments.topLevelAwait = true\r\n config.resolve.fallback = {\r\n ...config.resolve.fallback,\r\n fs: false,\r\n }\r\n\r\n return config\r\n },\r\n async headers() {\r\n return [\r\n {\r\n source: \"/:path*\",\r\n headers: securityHeaders,\r\n },\r\n ]\r\n },\r\n}\r\n\r\nmodule.exports = withSentryConfig(nextConfig, {\r\n // silent: true,\r\n})\r\n```\r\n`pages/_error.tsx`\r\n```ts\r\n/**\r\n * NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.\r\n *\r\n * This page is loaded by Nextjs:\r\n * - on the server, when data-fetching methods throw or reject\r\n * - on the client, when `getInitialProps` throws or rejects\r\n * - on the client, when a React lifecycle method throws or rejects, and it's\r\n * caught by the built-in Nextjs error boundary\r\n *\r\n * See:\r\n * - https://nextjs.org/docs/basic-features/data-fetching/overview\r\n * - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props\r\n * - https://reactjs.org/docs/error-boundaries.html\r\n */\r\n\r\nimport * as Sentry from \"@sentry/nextjs\"\r\nimport type { NextPage } from \"next\"\r\nimport type { ErrorProps } from \"next/error\"\r\nimport NextErrorComponent from \"next/error\"\r\n\r\nconst CustomErrorComponent: NextPage\u003cErrorProps> = (props) => {\r\n // If you're using a Nextjs version prior to 12.2.1, uncomment this to\r\n // compensate for https://github.com/vercel/next.js/issues/8592\r\n // Sentry.captureUnderscoreErrorException(props);\r\n\r\n return \u003cNextErrorComponent statusCode={props.statusCode} />\r\n}\r\n\r\nCustomErrorComponent.getInitialProps = async (contextData) => {\r\n // In case this is running in a serverless function, await this in order to give Sentry\r\n // time to send the error before the lambda exits\r\n await Sentry.captureUnderscoreErrorException(contextData)\r\n\r\n // This will contain the status code of the response\r\n return NextErrorComponent.getInitialProps(contextData)\r\n}\r\n\r\nexport default CustomErrorComponent\r\n```\r\n\r\n`.sentryclirc`\r\n```\r\n[auth]\r\ntoken=xxxxxxxxxxxxxxx\r\n```\r\n\r\n`sentry.prooperties`\r\n```\r\ndefaults.url=https://sentry.io/\r\ndefaults.org=xxxx\r\ndefaults.project=javascript-nextjs\r\ncli.executable=node_modules/@sentry/cli/bin/sentry-cli\r\n```\r\n\r\n\r\n\r\nBelow is the log displayed on the console.\r\n```bash\r\ninfo - @sentry/nextjs is running with the autoInstrumentServerFunctions flag set, which means API routes no longer need to be manually wrapped with withSentry. Detected manual wrapping in /api/test.\r\nwait - compiling /_error (client and server)...\r\nerror - pages/api/test.ts (6:8) @ handle\r\nerror - Error: API Test 1\r\n at handle (webpack-internal:///(api)/./pages/api/test.ts?__sentry_wrapped__:12:11)\r\n at sentryWrappedHandler (/Users/asdf/Projects/test-web/node_modules/@sentry/nextjs/cjs/config/wrappers/withSentryAPI.js:62:14)\r\n at /Users/asdfProjects/test-web/node_modules/@sentry/nextjs/cjs/config/wrappers/withSentryAPI.js:132:37\r\n at bound (node:domain:433:15)\r\n at runBound (node:domain:444:12)\r\n at sentryWrappedHandler (/Users/asdf/Projects/test-web/node_modules/@sentry/nextjs/cjs/config/wrappers/withSentryAPI.js:196:12)\r\n at Object.apiResolver (/Users/asdf/Projects/test-web/node_modules/next/dist/server/api-utils/node.js:363:15)\r\n at runMicrotasks (\u003canonymous>)\r\n at processTicksAndRejections (node:internal/process/task_queues:96:5)\r\n at async DevServer.runApi (/Users/asdf/Projects/test-web/node_modules/next/dist/server/next-server.js:474:9) {\r\n page: '/api/test'\r\n}\r\n 4 | \r\n 5 | const handle = async (req: NextApiRequest, res: NextApiResponse) => {\r\n> 6 | throw new Error(\"API Test 1\")\r\n | ^\r\n 7 | \r\n 8 | return res.status(OK).end()\r\n 9 | }\r\n[Sentry Webpack Plugin] Creating new release:\r\n 'development'\r\n[Sentry Webpack Plugin] Calling upload-sourcemaps with:\r\n {\r\n finalize: true,\r\n rewrite: true,\r\n include: [\r\n {\r\n paths: [\r\n '/Users/asdf/Projects/test-web/.next/static/chunks/pages'\r\n ],\r\n urlPrefix: '~/_next/static/chunks/pages'\r\n }\r\n ],\r\n ignore: [],\r\n configFile: 'sentry.properties',\r\n stripPrefix: [ 'webpack://_N_E/' ],\r\n urlPrefix: '~/_next',\r\n entries: [Function: entries],\r\n release: 'development',\r\n dryRun: true\r\n}\r\n[Sentry Webpack Plugin] Finalizing release:\r\n 'development'\r\n[Sentry Webpack Plugin] Creating new release:\r\n 'development'\r\n[Sentry Webpack Plugin] Calling upload-sourcemaps with:\r\n {\r\n finalize: true,\r\n rewrite: true,\r\n include: [\r\n {\r\n paths: [ '/Users/asdf/Projects/test-web/.next/server/pages/' ],\r\n urlPrefix: '~/_next/server/pages'\r\n },\r\n {\r\n paths: [ '/Users/asdf/Projects/test-web/.next/server/chunks/' ],\r\n urlPrefix: '~/_next/server/chunks'\r\n }\r\n ],\r\n ignore: [],\r\n configFile: 'sentry.properties',\r\n stripPrefix: [ 'webpack://_N_E/' ],\r\n urlPrefix: '~/_next',\r\n entries: [Function: entries],\r\n release: 'development',\r\n dryRun: true\r\n}\r\n[Sentry Webpack Plugin] Finalizing release:\r\n 'development'\r\nevent - compiled client and server successfully in 538 ms (1680 modules)\r\n```\r\n\r\n### Expected Result\r\n\r\n@sentry/nextjs should be reported to Sentry about API errors.\r\n\r\n\r\n### Actual Result\r\n\r\n@sentry/nextjs not reporting errors in next.js api routes\r\n\r\n### Related Issues\r\nhttps://github.com/getsentry/sentry-javascript/issues/3917","author":{"url":"https://github.com/iCodePoet","@type":"Person","name":"iCodePoet"},"datePublished":"2022-12-07T10:04:26Z","url":"https://github.com/getsentry/sentry-javascript/issues/6450"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:30b14859-78c6-c3b2-00d1-6f2075fbd7ad |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBEE:175E8E:37DEA:4BF5C:6B04A2DF |
| html-safe-nonce | 3ca18c83ab61a65fc5e0d846942705feeecfcb56d2db6c440d6c2694f4867bea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkVFOjE3NUU4RTozN0RFQTo0QkY1Qzo2QjA0QTJERiIsInZpc2l0b3JfaWQiOiIzMTI5NDcyNTEyNjUyNzgwMjU1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 455bf0e5e5cef4ce4da5493cd1ceeba07b90527988cdf47df899c298b62e791f |
| hovercard-subject-tag | issue:1481516253 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/6450/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e2a4bd42ed92fc44cafee9687fc789796c6eea9eb1e9b055d18708e8b7f5dd21/getsentry/sentry-javascript/issues/6450 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e2a4bd42ed92fc44cafee9687fc789796c6eea9eb1e9b055d18708e8b7f5dd21/getsentry/sentry-javascript/issues/6450 |
| og:image:alt | Version @sentry/nextjs: 7.23.0 next: 13.0.6 Issue @sentry/nextjs not reporting errors in next.js api routes Only errors from the font-end are reported to Sentry. Do not report API errors to Sentry.... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iCodePoet |
| hostname | github.com |
| expected-hostname | github.com |
| None | 188497a0f45a7aa8f7c866faa1950d1a7f8d82b59416adf549b222ad1d1dd19b |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 557cfbac6cdb532730c95c90f2cefdc0c4128029 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width