simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 1 | import * as Sentry from '@sentry/node'; |
| 2 | import * as Tracing from '@sentry/tracing'; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 3 | // import config from "./sentry-server.config.json" assert { type: "json" }; |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 4 | |
| 5 | export function sentrySetUp(app) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 6 | Sentry.init({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 7 | integrations: [ |
| 8 | // enable HTTP calls tracing |
| 9 | new Sentry.Integrations.Http({ tracing: true }), |
| 10 | // enable Express.js middleware tracing |
| 11 | new Tracing.Integrations.Express({ app }), |
| 12 | ], |
| 13 | }); |
| 14 | |
| 15 | // RequestHandler creates a separate execution context using domains, so that every |
| 16 | // transaction/span/breadcrumb is attached to its own Hub instance |
| 17 | app.use(Sentry.Handlers.requestHandler()); |
| 18 | // TracingHandler creates a trace for every incoming request |
| 19 | app.use(Sentry.Handlers.tracingHandler()); |
| 20 | |
| 21 | app.get('/debug-sentry', function mainHandler(req, res) { |
| 22 | throw new Error('My first Sentry error!'); |
| 23 | }); |
| 24 | |
| 25 | // The error handler must be before any other error middleware and after all controllers |
| 26 | // app.use(Sentry.Handlers.errorHandler()); |
| 27 | app.use( |
| 28 | Sentry.Handlers.errorHandler({ |
| 29 | shouldHandleError(error) { |
| 30 | // Capture all 404 and 500 errors |
| 31 | if (error.status === 404 || error.status === 500) { |
| 32 | return true; |
| 33 | } |
| 34 | return false; |
| 35 | }, |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 36 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 37 | ); |
| 38 | // Optional fallthrough error handler |
| 39 | app.use(function onError(err, req, res, next) { |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 40 | // The error id is attached to `res.sentry` to be returned |
| 41 | // and optionally displayed to the user for support. |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 42 | res.statusCode = 500; |
| 43 | res.end(res.sentry + '\n'); |
| 44 | }); |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 45 | } |