blob: 55d9ba2148e267617e3b1b2f5c02f286441e8aac [file] [log] [blame]
simond47ef9e2022-09-28 22:24:28 -04001import * as Sentry from '@sentry/node';
2import * as Tracing from '@sentry/tracing';
idillon531b6f22022-09-16 14:02:00 -04003// import config from "./sentry-server.config.json" assert { type: "json" };
idillon452e2102022-09-16 13:23:28 -04004
5export function sentrySetUp(app) {
simond47ef9e2022-09-28 22:24:28 -04006 Sentry.init({
simond47ef9e2022-09-28 22:24:28 -04007 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 },
idillon452e2102022-09-16 13:23:28 -040036 })
simond47ef9e2022-09-28 22:24:28 -040037 );
38 // Optional fallthrough error handler
39 app.use(function onError(err, req, res, next) {
idillon452e2102022-09-16 13:23:28 -040040 // The error id is attached to `res.sentry` to be returned
41 // and optionally displayed to the user for support.
simond47ef9e2022-09-28 22:24:28 -040042 res.statusCode = 500;
43 res.end(res.sentry + '\n');
44 });
idillon452e2102022-09-16 13:23:28 -040045}