blob: 320ca37d1e11842e692bc5563730fd79792db735 [file] [log] [blame]
idillon452e2102022-09-16 13:23: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) {
6 Sentry.init({
7 ...config,
8 integrations: [
9 // enable HTTP calls tracing
10 new Sentry.Integrations.Http({ tracing: true }),
11 // enable Express.js middleware tracing
12 new Tracing.Integrations.Express({ app }),
13 ],
14 });
15
16 // RequestHandler creates a separate execution context using domains, so that every
17 // transaction/span/breadcrumb is attached to its own Hub instance
18 app.use(Sentry.Handlers.requestHandler());
19 // TracingHandler creates a trace for every incoming request
20 app.use(Sentry.Handlers.tracingHandler());
21
22 app.get("/debug-sentry", function mainHandler(req, res) {
23 throw new Error("My first Sentry error!");
24 });
25
26 // The error handler must be before any other error middleware and after all controllers
27 // app.use(Sentry.Handlers.errorHandler());
28 app.use(
29 Sentry.Handlers.errorHandler({
30 shouldHandleError(error) {
31 // Capture all 404 and 500 errors
32 if (error.status === 404 || error.status === 500) {
33 return true;
34 }
35 return false;
36 },
37 })
38 );
39 // Optional fallthrough error handler
40 app.use(function onError(err, req, res, next) {
41 // The error id is attached to `res.sentry` to be returned
42 // and optionally displayed to the user for support.
43 res.statusCode = 500;
44 res.end(res.sentry + "\n");
45 });
46}