Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[meta] Fix Sentry error reporting #511

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 62 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/zmarkdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 3 additions & 15 deletions packages/zmarkdown/server/factories/controller-factory.js
Original file line number Diff line number Diff line change
@@ -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 || {}
Expand All @@ -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
Expand Down Expand Up @@ -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))
}
20 changes: 15 additions & 5 deletions packages/zmarkdown/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down