simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 18 | import * as Sentry from '@sentry/node'; |
| 19 | import * as Tracing from '@sentry/tracing'; |
idillon | 531b6f2 | 2022-09-16 14:02:00 -0400 | [diff] [blame] | 20 | // import config from "./sentry-server.config.json" assert { type: "json" }; |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 21 | |
| 22 | export function sentrySetUp(app) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 23 | Sentry.init({ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 24 | integrations: [ |
| 25 | // enable HTTP calls tracing |
| 26 | new Sentry.Integrations.Http({ tracing: true }), |
| 27 | // enable Express.js middleware tracing |
| 28 | new Tracing.Integrations.Express({ app }), |
| 29 | ], |
| 30 | }); |
| 31 | |
| 32 | // RequestHandler creates a separate execution context using domains, so that every |
| 33 | // transaction/span/breadcrumb is attached to its own Hub instance |
| 34 | app.use(Sentry.Handlers.requestHandler()); |
| 35 | // TracingHandler creates a trace for every incoming request |
| 36 | app.use(Sentry.Handlers.tracingHandler()); |
| 37 | |
Misha Krieger-Raynauld | 173fbba | 2022-11-16 14:10:38 -0500 | [diff] [blame] | 38 | app.get('/debug-sentry', function mainHandler(_req, _res) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 39 | throw new Error('My first Sentry error!'); |
| 40 | }); |
| 41 | |
| 42 | // The error handler must be before any other error middleware and after all controllers |
| 43 | // app.use(Sentry.Handlers.errorHandler()); |
| 44 | app.use( |
| 45 | Sentry.Handlers.errorHandler({ |
| 46 | shouldHandleError(error) { |
| 47 | // Capture all 404 and 500 errors |
| 48 | if (error.status === 404 || error.status === 500) { |
| 49 | return true; |
| 50 | } |
| 51 | return false; |
| 52 | }, |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 53 | }) |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 54 | ); |
| 55 | // Optional fallthrough error handler |
Misha Krieger-Raynauld | 173fbba | 2022-11-16 14:10:38 -0500 | [diff] [blame] | 56 | app.use(function onError(_err, _req, res, _next) { |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 57 | // The error id is attached to `res.sentry` to be returned |
| 58 | // and optionally displayed to the user for support. |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | res.statusCode = 500; |
| 60 | res.end(res.sentry + '\n'); |
| 61 | }); |
idillon | 452e210 | 2022-09-16 13:23:28 -0400 | [diff] [blame] | 62 | } |