| /* |
| * Copyright (C) 2022 Savoir-faire Linux Inc. |
| * |
| * This program is free software; you can redistribute it and/or modify |
| * it under the terms of the GNU Affero General Public License as |
| * published by the Free Software Foundation; either version 3 of the |
| * License, or (at your option) any later version. |
| * |
| * This program is distributed in the hope that it will be useful, |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| * GNU Affero General Public License for more details. |
| * |
| * You should have received a copy of the GNU Affero General Public |
| * License along with this program. If not, see |
| * <https://www.gnu.org/licenses/>. |
| */ |
| import * as Sentry from '@sentry/node'; |
| import * as Tracing from '@sentry/tracing'; |
| // import config from "./sentry-server.config.json" assert { type: "json" }; |
| |
| export function sentrySetUp(app) { |
| Sentry.init({ |
| integrations: [ |
| // enable HTTP calls tracing |
| new Sentry.Integrations.Http({ tracing: true }), |
| // enable Express.js middleware tracing |
| new Tracing.Integrations.Express({ app }), |
| ], |
| }); |
| |
| // RequestHandler creates a separate execution context using domains, so that every |
| // transaction/span/breadcrumb is attached to its own Hub instance |
| app.use(Sentry.Handlers.requestHandler()); |
| // TracingHandler creates a trace for every incoming request |
| app.use(Sentry.Handlers.tracingHandler()); |
| |
| app.get('/debug-sentry', function mainHandler(_req, _res) { |
| throw new Error('My first Sentry error!'); |
| }); |
| |
| // The error handler must be before any other error middleware and after all controllers |
| // app.use(Sentry.Handlers.errorHandler()); |
| app.use( |
| Sentry.Handlers.errorHandler({ |
| shouldHandleError(error) { |
| // Capture all 404 and 500 errors |
| if (error.status === 404 || error.status === 500) { |
| return true; |
| } |
| return false; |
| }, |
| }) |
| ); |
| // Optional fallthrough error handler |
| app.use(function onError(_err, _req, res, _next) { |
| // The error id is attached to `res.sentry` to be returned |
| // and optionally displayed to the user for support. |
| res.statusCode = 500; |
| res.end(res.sentry + '\n'); |
| }); |
| } |