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).
Preserve the original error when logging or re-throwing
Don't discard the caught error. Include its message (and stack/cause) in the log and in any wrapper error so failures stay diagnosable.
Bad example
| 1 | try { |
| 2 | await connectToRedis(); |
| 3 | } catch (error) { |
| 4 | logger.error('Failed to connect to Redis'); |
| 5 | throw new Error('Failed to connect to Redis'); |
| 6 | } |
Explanation (EN)
Both the log line and the thrown error throw away the original error, so there is no message, stack, or root cause to debug from — every failure looks identical.
Objašnjenje (HR)
I log linija i bacena greska odbacuju originalnu gresku, pa nema poruke, stacka ni korijenskog uzroka za debugiranje — svaki kvar izgleda identicno.
Good example
| 1 | try { |
| 2 | await connectToRedis(); |
| 3 | } catch (error) { |
| 4 | logger.error(`Failed to connect to Redis: ${error.message}`, error.stack); |
| 5 | // In ES2022+ runtimes, chain the cause so the original is preserved: |
| 6 | throw new Error('Failed to connect to Redis', { cause: error }); |
| 7 | } |
Explanation (EN)
Including error.message and error.stack in the log and chaining the original via the Error cause option keeps the root cause attached all the way up the call stack.
Objašnjenje (HR)
Ukljucivanje error.message i error.stack u log te lancanje originala preko Error cause opcije zadrzava korijenski uzrok prikvacen sve do vrha poziva.
Exceptions / Tradeoffs (EN)
When deliberately mapping an internal error to a sanitized client-facing message, keep the original in server logs/cause but do not leak it in the API response.
Iznimke / Tradeoffi (HR)
Kad namjerno mapirate internu gresku u sanitiziranu poruku za klijenta, zadrzite original u server logovima/cause-u ali ga ne otkrivajte u API odgovoru.