|
| 1 | +import './instrument'; |
| 2 | + |
| 3 | +// Other imports below |
| 4 | +import * as Sentry from '@sentry/node'; |
| 5 | +import express from 'express'; |
| 6 | + |
| 7 | +const app = express(); |
| 8 | +const port = 3030; |
| 9 | + |
| 10 | +app.get('/test-success', function (req, res) { |
| 11 | + res.send({ version: 'v1' }); |
| 12 | +}); |
| 13 | + |
| 14 | +app.get('/test-param/:param', function (req, res) { |
| 15 | + res.send({ paramWas: req.params.param }); |
| 16 | +}); |
| 17 | + |
| 18 | +app.get('/test-transaction', function (req, res) { |
| 19 | + Sentry.withActiveSpan(null, async () => { |
| 20 | + Sentry.startSpan({ name: 'test-transaction', op: 'e2e-test' }, () => { |
| 21 | + Sentry.startSpan({ name: 'test-span' }, () => undefined); |
| 22 | + }); |
| 23 | + |
| 24 | + await Sentry.flush(); |
| 25 | + |
| 26 | + res.send({}); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +app.get('/test-error', async function (req, res) { |
| 31 | + const exceptionId = Sentry.captureException(new Error('This is an error')); |
| 32 | + |
| 33 | + await Sentry.flush(2000); |
| 34 | + |
| 35 | + res.send({ exceptionId }); |
| 36 | +}); |
| 37 | + |
| 38 | +app.get('/test-exception/:id', function (req, _res) { |
| 39 | + throw new Error(`This is an exception with id ${req.params.id}`); |
| 40 | +}); |
| 41 | + |
| 42 | +Sentry.setupExpressErrorHandler(app); |
| 43 | + |
| 44 | +app.use(function onError(err: unknown, req: any, res: any, next: any) { |
| 45 | + // The error id is attached to `res.sentry` to be returned |
| 46 | + // and optionally displayed to the user for support. |
| 47 | + res.statusCode = 500; |
| 48 | + res.end(res.sentry + '\n'); |
| 49 | +}); |
| 50 | + |
| 51 | +app.listen(port, () => { |
| 52 | + console.log(`Example app listening on port ${port}`); |
| 53 | +}); |
0 commit comments