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).
Land process-level crash handlers together with guards at the known throw sites
Adding a global handler that exits the process turns every pre-existing unguarded throw into a hard outage, so ship it in the same change as guards for the throw sites you already know about.
Bad example
| 1 | installCrashHandlers(); // uncaughtException / unhandledRejection -> process.exit(1) |
| 2 |
|
| 3 | // ...elsewhere, unchanged: |
| 4 | subscriber.on("message", (raw) => { |
| 5 | const parsed = JSON.parse(raw); // one malformed message now kills the process |
| 6 | handle(parsed); |
| 7 | }); |
Explanation (EN)
The handler is correct in isolation, but it converts a previously survivable parse error into a process exit that drops every connected client.
Objašnjenje (HR)
Handler je sam po sebi ispravan, ali pretvara gresku parsiranja koja se prije prezivjela u gasenje procesa koje ruzi sve spojene klijente.
Good example
| 1 | installCrashHandlers(); |
| 2 |
|
| 3 | subscriber.on("message", (raw) => { |
| 4 | let parsed; |
| 5 | try { |
| 6 | parsed = JSON.parse(raw); |
| 7 | } catch (error) { |
| 8 | logger.warn("Dropping malformed message", { error }); |
| 9 | return; |
| 10 | } |
| 11 | handle(parsed); |
| 12 | }); |
Explanation (EN)
The known throw site is guarded in the same change, so the crash handler catches genuinely unexpected failures rather than routine bad input.
Objašnjenje (HR)
Poznato mjesto bacanja je zasticeno u istoj promjeni, pa crash handler hvata stvarno neocekivane kvarove, a ne uobicajeni losi ulaz.
Notes (EN)
Before adding the handlers, grep for unguarded JSON.parse, writes to sockets with no 'error' listener, and floating promises at boot. Those are the sites that become fatal first.
Bilješke (HR)
Prije dodavanja handlera potrazi nezasticene JSON.parse, pisanje u socket bez 'error' slusatelja i lebdece promise-e pri pokretanju. To su mjesta koja prva postaju fatalna.