From 4e46f74da01bd516d829ad326c71b26d90e2ca68 Mon Sep 17 00:00:00 2001 From: Lika Kavkasidze Date: Wed, 24 Apr 2024 21:35:01 +0200 Subject: [PATCH] [meta] Fix Sentry error reporting (#511) --- package-lock.json | 90 +++++++++++++------ packages/zmarkdown/package.json | 2 +- .../server/factories/controller-factory.js | 18 +--- packages/zmarkdown/server/index.js | 20 +++-- 4 files changed, 81 insertions(+), 49 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5fb82043..b3736757 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4677,58 +4677,73 @@ } }, "node_modules/@sentry-internal/tracing": { - "version": "7.110.1", - "resolved": "https://registry.npmjs.org/@sentry-internal/tracing/-/tracing-7.110.1.tgz", - "integrity": "sha512-4kTd6EM0OP1SVWl2yLn3KIwlCpld1lyhNDeR8G1aKLm1PN+kVsR6YB/jy9KPPp4Q3lN3W9EkTSES3qhP4jVffQ==", + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry-internal/tracing/-/tracing-7.112.2.tgz", + "integrity": "sha512-fT1Y46J4lfXZkgFkb03YMNeIEs2xS6jdKMoukMFQfRfVvL9fSWEbTgZpHPd/YTT8r2i082XzjtAoQNgklm/0Hw==", "dependencies": { - "@sentry/core": "7.110.1", - "@sentry/types": "7.110.1", - "@sentry/utils": "7.110.1" + "@sentry/core": "7.112.2", + "@sentry/types": "7.112.2", + "@sentry/utils": "7.112.2" }, "engines": { "node": ">=8" } }, "node_modules/@sentry/core": { - "version": "7.110.1", - "resolved": "https://registry.npmjs.org/@sentry/core/-/core-7.110.1.tgz", - "integrity": "sha512-yC1yeUFQlmHj9u/KxKmwOMVanBmgfX+4MZnZU31QPqN95adyZTwpaYFZl4fH5kDVnz7wXJI0qRP8SxuMePtqhw==", + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry/core/-/core-7.112.2.tgz", + "integrity": "sha512-gHPCcJobbMkk0VR18J65WYQTt3ED4qC6X9lHKp27Ddt63E+MDGkG6lvYBU1LS8cV7CdyBGC1XXDCfor61GvLsA==", "dependencies": { - "@sentry/types": "7.110.1", - "@sentry/utils": "7.110.1" + "@sentry/types": "7.112.2", + "@sentry/utils": "7.112.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@sentry/integrations": { + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry/integrations/-/integrations-7.112.2.tgz", + "integrity": "sha512-ioC2yyU6DqtLkdmWnm87oNvdn2+9oKctJeA4t+jkS6JaJ10DcezjCwiLscX4rhB9aWJV3IWF7Op0O6K3w0t2Hg==", + "dependencies": { + "@sentry/core": "7.112.2", + "@sentry/types": "7.112.2", + "@sentry/utils": "7.112.2", + "localforage": "^1.8.1" }, "engines": { "node": ">=8" } }, "node_modules/@sentry/node": { - "version": "7.110.1", - "resolved": "https://registry.npmjs.org/@sentry/node/-/node-7.110.1.tgz", - "integrity": "sha512-n6sNzZJ/ChfyCI1FxuGWgloeevC8j2vax3vXM4IZrSIm5hS1d9L2oCJ4HEPuxGUxCkQ1f4kXPcdmNaQsWH0JBw==", + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry/node/-/node-7.112.2.tgz", + "integrity": "sha512-MNzkqER8jc2xOS3ArkCLH5hakzu15tcjeC7qjU7rQ1Ms4WuV+MG0docSRESux0/p23Qjzf9tZOc8C5Eq+Sxduw==", "dependencies": { - "@sentry-internal/tracing": "7.110.1", - "@sentry/core": "7.110.1", - "@sentry/types": "7.110.1", - "@sentry/utils": "7.110.1" + "@sentry-internal/tracing": "7.112.2", + "@sentry/core": "7.112.2", + "@sentry/integrations": "7.112.2", + "@sentry/types": "7.112.2", + "@sentry/utils": "7.112.2" }, "engines": { "node": ">=8" } }, "node_modules/@sentry/types": { - "version": "7.110.1", - "resolved": "https://registry.npmjs.org/@sentry/types/-/types-7.110.1.tgz", - "integrity": "sha512-sZxOpM5gfyxvJeWVvNpHnxERTnlqcozjqNcIv29SZ6wonlkekmxDyJ3uCuPv85VO54WLyA4uzskPKnNFHacI8A==", + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry/types/-/types-7.112.2.tgz", + "integrity": "sha512-kCMLt7yhY5OkWE9MeowlTNmox9pqDxcpvqguMo4BDNZM5+v9SEb1AauAdR78E1a1V8TyCzjBD7JDfXWhvpYBcQ==", "engines": { "node": ">=8" } }, "node_modules/@sentry/utils": { - "version": "7.110.1", - "resolved": "https://registry.npmjs.org/@sentry/utils/-/utils-7.110.1.tgz", - "integrity": "sha512-eibLo2m1a7sHkOHxYYmRujr3D7ek2l9sv26F1SLoQBVDF7Afw5AKyzPmtA1D+4M9P/ux1okj7cGj3SaBrVpxXA==", + "version": "7.112.2", + "resolved": "https://registry.npmjs.org/@sentry/utils/-/utils-7.112.2.tgz", + "integrity": "sha512-OjLh0hx0t1EcL4ZIjf+4svlmmP+tHUDGcr5qpFWH78tjmkPW4+cqPuZCZfHSuWcDdeiaXi8TnYoVRqDcJKK/eQ==", "dependencies": { - "@sentry/types": "7.110.1" + "@sentry/types": "7.112.2" }, "engines": { "node": ">=8" @@ -8851,7 +8866,6 @@ "version": "0.1.13", "resolved": "https://registry.npmjs.org/encoding/-/encoding-0.1.13.tgz", "integrity": "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==", - "dev": true, "optional": true, "dependencies": { "iconv-lite": "^0.6.2" @@ -8861,7 +8875,6 @@ "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", - "dev": true, "optional": true, "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" @@ -12045,6 +12058,11 @@ "node": ">=10" } }, + "node_modules/immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha512-XXOFtyqDjNDAQxVfYxuF7g9Il/IbWmmlQg2MYKOH8ExIT1qg6xc4zyS3HaEEATgs1btfzxq15ciUiY7gjSXRGQ==" + }, "node_modules/import-fresh": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.0.tgz", @@ -15998,6 +16016,14 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true }, + "node_modules/lie": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.1.1.tgz", + "integrity": "sha512-RiNhHysUjhrDQntfYSfY4MU24coXXdEOgw9WGcKHNeEwffDYbF//u87M1EWaMGzuFoSbqW0C9C6lEEhDOAswfw==", + "dependencies": { + "immediate": "~3.0.5" + } + }, "node_modules/lines-and-columns": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-2.0.4.tgz", @@ -16040,6 +16066,14 @@ "node": ">=6.11.5" } }, + "node_modules/localforage": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/localforage/-/localforage-1.10.0.tgz", + "integrity": "sha512-14/H1aX7hzBBmmh7sGPd+AOMkkIrHM3Z1PAyGgZigA1H1p5O5ANnMyWzvpAETtG68/dC4pC0ncy3+PPGzXZHPg==", + "dependencies": { + "lie": "3.1.1" + } + }, "node_modules/locate-path": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", @@ -26152,7 +26186,7 @@ "license": "MIT", "dependencies": { "@pm2/io": "^4.3.5", - "@sentry/node": "^7.19.0", + "@sentry/node": "^7.111.0", "body-parser": "^1.19.0", "clone": "^2.1.2", "cors": "^2.8.5", diff --git a/packages/zmarkdown/package.json b/packages/zmarkdown/package.json index 6df2ebbc..db7f7016 100644 --- a/packages/zmarkdown/package.json +++ b/packages/zmarkdown/package.json @@ -39,7 +39,7 @@ }, "dependencies": { "@pm2/io": "^4.3.5", - "@sentry/node": "^7.19.0", + "@sentry/node": "^7.111.0", "body-parser": "^1.19.0", "clone": "^2.1.2", "cors": "^2.8.5", diff --git a/packages/zmarkdown/server/factories/controller-factory.js b/packages/zmarkdown/server/factories/controller-factory.js index e0133304..659f38b1 100644 --- a/packages/zmarkdown/server/factories/controller-factory.js +++ b/packages/zmarkdown/server/factories/controller-factory.js @@ -1,10 +1,8 @@ -const Sentry = require('@sentry/node') - const processorFactory = require('./processor-factory') const manifest = require('../utils/manifest') const io = require('../factories/io-factory') -module.exports = (givenProc, template) => (req, res) => { +module.exports = (givenProc, template) => (req, res, next) => { // Gather data about the request const rawContent = req.body.md const options = req.body.opts || {} @@ -17,16 +15,6 @@ module.exports = (givenProc, template) => (req, res) => { if (!useTemplate) io[givenProc]() else io['latex-document']() - function sendResponse (e, vfile) { - if (e) { - Sentry.captureException(e, { req, vfile }) - res.status(500).json(vfile) - return - } - - res.json([vfile.contents, vfile.data, vfile.messages]) - } - let extractPromises // Get a collection of Promises to execute @@ -75,6 +63,6 @@ module.exports = (givenProc, template) => (req, res) => { if (manifestRender) return manifest.dispatch(vfiles, rawContent) else return vfiles[0] }) - .then(vfile => sendResponse(null, vfile)) - .catch(e => sendResponse(e)) + .then(vfile => res.json([vfile.contents, vfile.data, vfile.messages])) + .catch(e => next(e)) } diff --git a/packages/zmarkdown/server/index.js b/packages/zmarkdown/server/index.js index 7068c237..9cf411bc 100644 --- a/packages/zmarkdown/server/index.js +++ b/packages/zmarkdown/server/index.js @@ -6,17 +6,15 @@ const path = require('path') const zmdVersion = require('../package.json').version +const app = express() + Sentry.init({ dsn: process.env.SENTRY_DSN, release: process.env.SENTRY_RELEASE || zmdVersion, environment: process.env.SENTRY_ENVIRONMENT || process.env.ZDS_ENVIRONMENT }) -const app = express() - -// Sentry request handling app.use(Sentry.Handlers.requestHandler()) - app.use(cors()) // Expose an image for tests @@ -28,9 +26,21 @@ if (process.env.ZMD_ENV !== 'production') { app.use('/', require('./routes/endpoints')) app.use('/munin', require('./routes/munin')) -// Sentry error handling +// Sentry error handler app.use(Sentry.Handlers.errorHandler()) +// Custom error handler, called only if a route throws +app.use((err, req, res, next) => { + res.status(500).json({ + type: 'InternalServerError', + statusCode: 500, + errorMessage: err.toString(), + uniqueId: res.sentry + }) + // eslint-disable-next-line no-useless-return + return +}) + const server = app.listen(process.env.PORT || 27272, () => { const host = server.address().address const port = server.address().port