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).
Normalize caught errors before string interpolation in logs
Interpolating a raw error object yields `[object Object]` — extract `e.message` (guarding with `instanceof Error`) before logging.
Bad example
| 1 | someAsyncTask().catch((e) => { |
| 2 | logger.error(`[task] indexing failed: ${e}`); // logs "[object Object]" |
| 3 | }); |
Explanation (EN)
Template-literal interpolation calls Object.prototype.toString on a non-string error, producing '[object Object]' and discarding the message and stack trace you actually need for debugging.
Objašnjenje (HR)
Interpolacija u predlošku poziva Object.prototype.toString na ne-string grešci, što daje '[object Object]' i odbacuje poruku i stack trace koji ti zapravo trebaju za debugiranje.
Good example
| 1 | someAsyncTask().catch((e) => { |
| 2 | const error = e instanceof Error ? e.message : String(e); |
| 3 | logger.error(`[task] indexing failed: ${error}`); |
| 4 | }); |
Explanation (EN)
The instanceof guard extracts a readable message for real Errors and safely stringifies anything else, so logs are actually useful.
Objašnjenje (HR)
instanceof zaštita izvlači čitljivu poruku za prave Errore i sigurno pretvara u string sve ostalo, pa su logovi stvarno korisni.
Notes (EN)
In catch/`.catch` callbacks the value is typed `unknown` (or `any`) and may not be an Error at all, so the guard is required, not optional.
Bilješke (HR)
U catch/`.catch` callbackovima vrijednost je tipa `unknown` (ili `any`) i možda uopće nije Error, pa je zaštita obavezna, ne opcionalna.