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).
Standardize error handling across boundaries
Use a consistent error strategy (typed errors or Result objects) so callers can handle failures predictably and avoid ad‑hoc patterns.
Bad example
| 1 | export async function loadUser(id: string) { |
| 2 | if (!id) throw 'missing id'; |
| 3 | const user = await db.user.findById(id); |
| 4 | if (!user) throw new Error('not found'); |
| 5 | return user; |
| 6 | } |
Explanation (EN)
The function throws inconsistent error types, so callers must guess how to handle failures. This leads to fragile error handling and missed cases.
Objašnjenje (HR)
Funkcija baca nekonzistentne tipove grešaka pa pozivatelji moraju pogađati kako ih obraditi. To vodi krhkom error handlingu i propuštenim slučajevima.
Good example
| 1 | class AppError extends Error { |
| 2 | constructor(public code: 'BAD_REQUEST' | 'NOT_FOUND', message: string) { |
| 3 | super(message); |
| 4 | } |
| 5 | } |
| 6 |
|
| 7 | export async function loadUser(id: string) { |
| 8 | if (!id) throw new AppError('BAD_REQUEST', 'missing id'); |
| 9 | const user = await db.user.findById(id); |
| 10 | if (!user) throw new AppError('NOT_FOUND', 'user not found'); |
| 11 | return user; |
| 12 | } |
Explanation (EN)
A single error strategy makes failures predictable and easy to map to HTTP responses or UI states. Callers can switch on a known error code instead of parsing messages.
Objašnjenje (HR)
Jedna strategija grešaka čini padove predvidljivima i lako mapiranima na HTTP odgovore ili UI stanja. Pozivatelji mogu switchati na poznati kod umjesto parsiranja poruka.
Notes (EN)
Result/Either patterns are also valid if your codebase avoids exceptions.
Bilješke (HR)
Result/Either patterni su također validni ako codebase izbjegava exceptione.
Exceptions / Tradeoffs (EN)
Very small scripts may skip this, but shared modules should standardize errors.
Iznimke / Tradeoffi (HR)
Vrlo male skripte mogu preskočiti ovo, ali shared moduli trebaju standardizirati greške.