Skip to content

Commit

Permalink
chore: improve Sentry express error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 25, 2024
1 parent 5bc92d7 commit d91be2a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
5 changes: 0 additions & 5 deletions apps/backend/src/web/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as Sentry from "@sentry/node";
import { Application, Router } from "express";

import { errorHandler } from "../middlewares/errorHandler.js";
import { apiMiddleware as githubApiMiddleware } from "../middlewares/github.js";
import { subdomain } from "../util.js";
import auth from "./auth.js";
Expand All @@ -20,8 +18,5 @@ export const installApiRouter = (app: Application) => {
router.use(auth);
router.use(stripe);

Sentry.setupExpressErrorHandler(router);
router.use(errorHandler());

app.use(subdomain(router, "api"));
};
2 changes: 0 additions & 2 deletions apps/backend/src/web/app-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from "node:path";
import { invariant } from "@argos/util/invariant";
import * as Sentry from "@sentry/node";
import express, { Router, static as serveStatic } from "express";
import { rateLimit } from "express-rate-limit";

Expand Down Expand Up @@ -109,5 +108,4 @@ export const installAppRouter = async (app: express.Application) => {
});

app.use(subdomain(router, "app"));
Sentry.setupExpressErrorHandler(router);
};
4 changes: 4 additions & 0 deletions apps/backend/src/web/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import config from "@/config/index.js";

import { installApiRouter } from "./api/index.js";
import { installAppRouter } from "./app-router.js";
import { jsonErrorHandler } from "./middlewares/errorHandler.js";

const __dirname = fileURLToPath(new URL(".", import.meta.url));

Expand Down Expand Up @@ -90,5 +91,8 @@ export const createApp = async () => {
installApiRouter(app);
await installAppRouter(app);

Sentry.setupExpressErrorHandler(app);
app.use(jsonErrorHandler());

return app;
};
34 changes: 20 additions & 14 deletions apps/backend/src/web/middlewares/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { ErrorRequestHandler } from "express";

export function errorHandler() {
const handlers: ErrorRequestHandler[] = [
(error, _req, _res, next) => {
if (process.env["NODE_ENV"] !== "test") {
console.log(error, error.stack);
}
next(error);
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(error: unknown, _req, res, _next) => {
/**
* A middleware that sends JSON responses for errors if request accepts JSON.
*/
export function jsonErrorHandler(): ErrorRequestHandler {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (error: unknown, req, res, next) => {
if (error instanceof Error && process.env["NODE_ENV"] !== "test") {
console.log(error, error.stack);
}

if (req.accepts("json") === "json") {
const statusCode =
error instanceof Error &&
"statusCode" in error &&
Expand All @@ -24,18 +25,23 @@ export function errorHandler() {
typeof error.code === "number"
? error.code
: 500;

const message =
error instanceof Error ? error.message : "Internal Server Error";

const code =
error instanceof Error && "code" in error ? error.code : undefined;

res.status(statusCode);
return res.send({

res.send({
error: {
message,
code,
},
});
},
];
return handlers;
} else {
next(error);
}
};
}

0 comments on commit d91be2a

Please sign in to comment.