Skip to content

Commit

Permalink
Merge pull request #258 from dataforgoodfr/chore/D4G-259-track-server…
Browse files Browse the repository at this point in the history
…-error-in-sentry

chore(ogre-server): Log errors to sentry
  • Loading branch information
Baboo7 authored Mar 24, 2023
2 parents c86d2b0 + 2882876 commit c6343a7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"dependencies": {
"@prisma/client": "^3.12.0",
"@sendgrid/mail": "^7.6.2",
"@sentry/node": "^7.43.0",
"@sentry/tracing": "^7.43.0",
"body-parser": "^1.20.0",
"cookie": "^0.5.0",
"cookie-parser": "^1.4.6",
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/error-handling/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./utils";
35 changes: 35 additions & 0 deletions packages/server/src/error-handling/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";
import { Request, Response, Router } from "express";
import { getUserRequesting } from "../lib/express";

export { initErrorTracer, traceError, traceRequests };

const initErrorTracer = (app: Router) => {
Sentry.init({
dsn: process.env.SENTRY_DSN,
environment: process.env.SENTRY_ENV || "development",
integrations: [
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({ app }),
],
// TODO: adapt value in production.
tracesSampleRate: 1.0,
});
};

const traceRequests = (app: Router) => {
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
};

const traceError = (err: Error, req: Request, res: Response) => {
const data = Sentry.extractRequestData(req);
const user = getUserRequesting(res);
Sentry.captureException(err, {
user: {
id: user?.id?.toString(),
},
contexts: data,
});
};
3 changes: 3 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import { initWebSocket } from "./modules/websocket";
import { logger } from "./logger";
import { logError, setRequestId } from "./middlewares";
import { limiter } from "./middlewares/limit";
import { initErrorTracer, traceRequests } from "./error-handling";

const app = express();
initErrorTracer(app);

connectToDatase().then(seed);

traceRequests(app);
app.use(bodyParser.urlencoded({ extended: false }));
// Parse JSON bodies (as sent by API clients)
app.use(bodyParser.json());
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/middlewares/logError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextFunction, Request, Response } from "express";
import { traceError } from "../error-handling";
import { logger } from "../logger";
import {
BusinessError,
Expand All @@ -11,12 +12,13 @@ const isProd = process.env.NODE_ENV === "production";

const logError = (
err: Error,
_: Request,
req: Request,
res: Response,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
next: NextFunction
) => {
logger.error(err);
traceError(err, req, res);

if (err instanceof BusinessError) {
if (err.code === "USER_NOT_AUTHENTICATED") {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1724,7 +1724,7 @@
"@sentry/utils" "7.43.0"
tslib "^1.9.3"

"@sentry/node@^7.19.0":
"@sentry/node@^7.19.0", "@sentry/node@^7.43.0":
version "7.43.0"
resolved "https://registry.yarnpkg.com/@sentry/node/-/node-7.43.0.tgz#bcb553486ffe3f1413063e9125fd34f9d84277d2"
integrity sha512-oXaTBq6Bk8Qwsd46hhRU2MLEnjYqWI41nPJmXyAWkDSYQTP7sUe1qM8bCUdsRpPwQh955Vq9qCRfgMbN4lEoAQ==
Expand Down

0 comments on commit c6343a7

Please sign in to comment.