Skip to content

Commit ac2ff45

Browse files
committed
impr: better handling of mongodb errors (@fehmer)
1 parent 81f09b9 commit ac2ff45

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

backend/src/middlewares/error.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ async function errorHandlingMiddleware(
4646
uid: monkeyError.uid ?? req.ctx?.decodedToken?.uid,
4747
};
4848
let message = "Unknown error";
49+
let isDbError = false;
4950

5051
if (/ECONNREFUSED.*27017/i.test(error.message)) {
5152
message = "Could not connect to the database. It may be down.";
53+
isDbError = true;
5254
} else if (error instanceof URIError || error instanceof SyntaxError) {
5355
status = 400;
5456
message = "Unprocessable request";
@@ -73,27 +75,30 @@ async function errorHandlingMiddleware(
7375
errorId: string;
7476
};
7577

76-
try {
77-
await addLog(
78-
"system_error",
79-
`${status} ${errorId} ${error.message} ${error.stack}`,
80-
uid
81-
);
82-
await db.collection<DBError>("errors").insertOne({
83-
_id: errorId,
84-
timestamp: Date.now(),
85-
status: status,
86-
uid,
87-
message: error.message,
88-
stack: error.stack,
89-
endpoint: req.originalUrl,
90-
method: req.method,
91-
url: req.url,
92-
});
93-
} catch (e) {
94-
Logger.error("Logging to db failed.");
95-
Logger.error(getErrorMessage(e) ?? "Unknown error");
96-
console.error(e);
78+
if (!isDbError) {
79+
try {
80+
await addLog(
81+
"system_error",
82+
`${status} ${errorId} ${error.message} ${error.stack}`,
83+
uid
84+
);
85+
86+
await db.collection<DBError>("errors").insertOne({
87+
_id: errorId,
88+
timestamp: Date.now(),
89+
status: status,
90+
uid,
91+
message: error.message,
92+
stack: error.stack,
93+
endpoint: req.originalUrl,
94+
method: req.method,
95+
url: req.url,
96+
});
97+
} catch (e) {
98+
Logger.error("Logging to db failed.");
99+
Logger.error(getErrorMessage(e) ?? "Unknown error");
100+
console.error(e);
101+
}
97102
}
98103
} else {
99104
Logger.error(`Error: ${error.message} Stack: ${error.stack}`);

backend/src/server.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ import { createIndicies as leaderboardDbSetup } from "./dal/leaderboards";
1919
import { createIndicies as blocklistDbSetup } from "./dal/blocklist";
2020
import { createIndicies as connectionsDbSetup } from "./dal/connections";
2121
import { getErrorMessage } from "./utils/error";
22+
import { exit } from "process";
2223

2324
async function bootServer(port: number): Promise<Server> {
2425
try {
2526
Logger.info(`Starting server version ${version}`);
2627
Logger.info(`Starting server in ${process.env["MODE"]} mode`);
28+
29+
process.on("unhandledRejection", (err) => {
30+
const isDbError =
31+
err instanceof Error && /ECONNREFUSED.*27017/i.test(err.message);
32+
if (isDbError) {
33+
Logger.error("Failed to connect to database, ignore error");
34+
} else {
35+
Logger.error("Unhandled rejection: " + getErrorMessage(err));
36+
exit(-1);
37+
}
38+
});
39+
2740
Logger.info(`Connecting to database ${process.env["DB_NAME"]}...`);
2841
await db.connect();
2942
Logger.success("Connected to database");

0 commit comments

Comments
 (0)