Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve Sentry express error handling #1382

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/backend/src/auth/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AuthError extends Error {}
const getTokenFromAuthHeader = (authHeader: string) => {
const auth = authorization.parse(authHeader);
if (auth.scheme !== "Bearer") {
throw new AuthError("Invalid auth scheme");
throw new AuthError(`Invalid auth scheme: ${auth.scheme || "no scheme"}`);
}
if (typeof auth.token !== "string" || !auth.token) {
throw new AuthError("Invalid auth token");
Expand Down
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);
}
};
}