blob: 5dd25f686ac73937724e594d6270d0e8a67c6a36 [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simond47ef9e2022-09-28 22:24:28 -040018import * as Sentry from '@sentry/node';
19import * as Tracing from '@sentry/tracing';
idillon531b6f22022-09-16 14:02:00 -040020// import config from "./sentry-server.config.json" assert { type: "json" };
idillon452e2102022-09-16 13:23:28 -040021
22export function sentrySetUp(app) {
simond47ef9e2022-09-28 22:24:28 -040023 Sentry.init({
simond47ef9e2022-09-28 22:24:28 -040024 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-Raynauld173fbba2022-11-16 14:10:38 -050038 app.get('/debug-sentry', function mainHandler(_req, _res) {
simond47ef9e2022-09-28 22:24:28 -040039 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 },
idillon452e2102022-09-16 13:23:28 -040053 })
simond47ef9e2022-09-28 22:24:28 -040054 );
55 // Optional fallthrough error handler
Misha Krieger-Raynauld173fbba2022-11-16 14:10:38 -050056 app.use(function onError(_err, _req, res, _next) {
idillon452e2102022-09-16 13:23:28 -040057 // The error id is attached to `res.sentry` to be returned
58 // and optionally displayed to the user for support.
simond47ef9e2022-09-28 22:24:28 -040059 res.statusCode = 500;
60 res.end(res.sentry + '\n');
61 });
idillon452e2102022-09-16 13:23:28 -040062}