Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Do not keep a Node process alive after an uncaught exception
Log, exit, and rely on a restart policy; recovery after uncaught exceptions can leave unknown state.
Bad example
| 1 | // server.ts |
| 2 | import express from "express"; |
| 3 |
|
| 4 | process.on("uncaughtException", (err) => { |
| 5 | // eslint-disable-next-line no-console |
| 6 | console.error("uncaughtException", err); |
| 7 | // Trying to keep the process alive can leave unknown state. |
| 8 | }); |
| 9 |
|
| 10 | const app = express(); |
| 11 | app.get("/", (_req, res) => res.send("ok")); |
| 12 | app.listen(3000); |
Explanation (EN)
Continuing after an uncaught exception can leave the process in a corrupted or unsafe state, causing subtle data bugs.
Objašnjenje (HR)
Nastaviti rad nakon uncaught exceptiona može ostaviti proces u pokvarenom ili nesigurnom stanju i uzrokovati suptilne bugove.
Good example
| 1 | // server.ts |
| 2 | import express from "express"; |
| 3 |
|
| 4 | process.on("uncaughtException", (err) => { |
| 5 | // eslint-disable-next-line no-console |
| 6 | console.error("uncaughtException", err); |
| 7 | process.exit(1); |
| 8 | }); |
| 9 |
|
| 10 | process.on("unhandledRejection", (reason) => { |
| 11 | // eslint-disable-next-line no-console |
| 12 | console.error("unhandledRejection", reason); |
| 13 | process.exit(1); |
| 14 | }); |
| 15 |
|
| 16 | const app = express(); |
| 17 | app.get("/", (_req, res) => res.send("ok")); |
| 18 | app.listen(3000); |
Explanation (EN)
Fail fast is safer. With a restart policy (systemd/Docker/K8s/pm2), the process is replaced with a clean state.
Objašnjenje (HR)
Fail fast je sigurnije. Uz restart policy (systemd/Docker/K8s/pm2), proces se zamijeni čistim stanjem.
Exceptions / Tradeoffs (EN)
For CLI tools, you may handle and report errors without exiting immediately, but servers should prefer log-exit-restart.
Iznimke / Tradeoffi (HR)
Za CLI alate možeš obraditi i prikazati grešku bez trenutnog izlaza, ali serveri bi trebali preferirati log-exit-restart.