Title: Calling `Sentry.flush()` with `@sentry/node` always throws an exception on Next.js API endpoints · Issue #3748 · getsentry/sentry-javascript · GitHub
Open Graph Title: Calling `Sentry.flush()` with `@sentry/node` always throws an exception on Next.js API endpoints · Issue #3748 · getsentry/sentry-javascript
X Title: Calling `Sentry.flush()` with `@sentry/node` always throws an exception on Next.js API endpoints · Issue #3748 · getsentry/sentry-javascript
Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javascript/releases Provide a link to the aff...
Open Graph Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas...
X Description: Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/3748
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Calling `Sentry.flush()` with `@sentry/node` always throws an exception on Next.js API endpoints","articleBody":"\u003c!-- Requirements: please go through this checklist before opening a new issue -->\r\n\r\n- [x] Review the documentation: https://docs.sentry.io/\r\n- [x] Search for existing issues: https://github.com/getsentry/sentry-javascript/issues\r\n- [x] Use the latest release: https://github.com/getsentry/sentry-javascript/releases\r\n- [ ] Provide a link to the affected event from your Sentry account\r\n\r\n## Package + Version\r\n\r\n- [x] `@sentry/browser` 6.3.6\r\n- [x] `@sentry/node` 6.3.6\r\n- [ ] `raven-js`\r\n- [ ] `raven-node` _(raven for node)_\r\n- [ ] other:\r\n\r\n### Version:\r\n\r\n```\r\n0.0.0\r\n```\r\n\r\n## Description\r\n\r\n\r\nIt's recommended by Next.js to call `Sentry.flush()` manually, before returning an HTTP response from API endpoints.\r\n\r\nBut, it doesn't work so well. When calling the API endpoint, it doesn't send the error to Sentry and fails with `[flushSafe] An exception was thrown while running Sentry.flush()`. _(see implementation below)_\r\n\r\nThe timeout being used is `5000`, which is more than what's recommended (`2000`).\r\n\r\nWhat's also weird is that I wasn't flushing before and it was working (at least sometimes) but now it always fails. \r\n\r\n```\r\n2021-06-24T20:33:32.151Z [modules/core/amplitude/amplitudeServerClient] Logging Amplitude event \"api-invoked\" with properties: { apiEndpoint: 'error' }\r\n2021-06-24T20:33:52.953Z\t3751ccb9-311e-4e5d-87b1-6b1a1aa04ce9\tERROR\t2021-06-24T20:33:52.397Z [api/error] Fake error - Sentry test from /api/error\r\n2021-06-24T20:33:52.953Z\t3751ccb9-311e-4e5d-87b1-6b1a1aa04ce9\tERROR\t2021-06-24T20:33:35.287Z [modules/core/sentry/universal] [flushSafe] An exception was thrown while running Sentry.flush() false\r\n``` \r\n\r\n```ts\r\nimport { logEvent } from '@/modules/core/amplitude/amplitudeServerClient';\r\nimport {\r\n AMPLITUDE_API_ENDPOINTS,\r\n AMPLITUDE_EVENTS,\r\n} from '@/modules/core/amplitude/events';\r\nimport { createLogger } from '@/modules/core/logging/logger';\r\nimport { configureReq } from '@/modules/core/sentry/server';\r\nimport { flushSafe } from '@/modules/core/sentry/universal';\r\nimport * as Sentry from '@sentry/node';\r\nimport {\r\n NextApiRequest,\r\n NextApiResponse,\r\n} from 'next';\r\n\r\nconst fileLabel = 'api/error';\r\nconst logger = createLogger({\r\n fileLabel,\r\n});\r\n\r\n/**\r\n * Error endpoint - Throws an error upon being called.\r\n *\r\n * Mock API endpoint whose sole purpose is to throw an error, nothing else.\r\n * Meant to be used to check whether monitoring (Sentry) works as expected.\r\n *\r\n * @param req\r\n * @param res\r\n * @method GET\r\n */\r\nexport const error = async (req: NextApiRequest, res: NextApiResponse): Promise\u003cvoid> => {\r\n try {\r\n configureReq(req, { fileLabel });\r\n\r\n await logEvent(AMPLITUDE_EVENTS.API_INVOKED, null, {\r\n apiEndpoint: AMPLITUDE_API_ENDPOINTS.ERROR,\r\n });\r\n\r\n throw Error('Fake error - Sentry test from /api/error');\r\n } catch (e) {\r\n Sentry.captureException(e);\r\n logger.error(e.message);\r\n\r\n await flushSafe();\r\n\r\n res.json({\r\n error: true,\r\n message: process.env.NEXT_PUBLIC_APP_STAGE === 'production' ? undefined : e.message,\r\n });\r\n }\r\n};\r\n\r\n/**\r\n * Flushes Sentry queue in a safe way.\r\n *\r\n * It's necessary to flush all Sentry events on the server, because Vercel runs on AWS Lambda, see https://vercel.com/docs/platform/limits#streaming-responses\r\n * If you don't flush, then it's possible the Sentry events won't be sent.\r\n * This helper is meant to be used for backend-only usage. (not frontend)\r\n *\r\n * There is a potential bug in Sentry that throws an exception when flushing times out, causing API endpoints to fail.\r\n * @see https://github.com/getsentry/sentry/issues/26870\r\n */\r\nexport const flushSafe = async (): Promise\u003cboolean> => {\r\n try {\r\n return await Sentry.flush(FLUSH_TIMEOUT);\r\n } catch (e) {\r\n logger.error(`[flushSafe] An exception was thrown while running Sentry.flush()`, e);\r\n return false;\r\n }\r\n};\r\n\r\nexport default error;\r\n\r\n```\r\n\r\n\r\n","author":{"url":"https://github.com/Vadorequest","@type":"Person","name":"Vadorequest"},"datePublished":"2021-06-24T21:08:18Z","url":"https://github.com/getsentry/sentry-javascript/issues/3748"}
| 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:42534a46-4bd2-1157-fcc4-de0b1835adec |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E9A:3142F0:602982:8729BC:6B0449C1 |
| html-safe-nonce | c1904393b7c92608ed581f74f1d45a5556b6e47cb91f1f23e5cc926ce854cf78 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTlBOjMxNDJGMDo2MDI5ODI6ODcyOUJDOjZCMDQ0OUMxIiwidmlzaXRvcl9pZCI6IjE4Mjg5ODkyOTMzOTg2MDgzMjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 13416a77412d8a067388ba0893f75c079ba5d700e5ac580ac56cde4d53bb2f97 |
| hovercard-subject-tag | issue:929613424 |
| 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/3748/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c518732844b295bce3ebdadfdede4f8afcf245240e5e8ada41243d802a980e15/getsentry/sentry-javascript/issues/3748 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c518732844b295bce3ebdadfdede4f8afcf245240e5e8ada41243d802a980e15/getsentry/sentry-javascript/issues/3748 |
| og:image:alt | Review the documentation: https://docs.sentry.io/ Search for existing issues: https://github.com/getsentry/sentry-javascript/issues Use the latest release: https://github.com/getsentry/sentry-javas... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Vadorequest |
| hostname | github.com |
| expected-hostname | github.com |
| None | d4495c3631faba668036f176d1f5946901c5172efd73a6edb984029e7d4f8e46 |
| 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 | 9ac4337fb7c6a5846cdfdb1a04f154d3c33ebc77 |
| ui-target | full |
| 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