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).
Propagate errors from fetch loops instead of logging and breaking
Don't swallow errors inside pagination/retry loops by logging and breaking; re-throw so callers see the failure rather than silently truncated data.
Bad example
| 1 | async function fetchAll(params: Params): Promise<Item[]> { |
| 2 | const results: Item[] = []; |
| 3 | let cursor: string | undefined; |
| 4 |
|
| 5 | do { |
| 6 | try { |
| 7 | const page = await api.getPage({ ...params, cursor }); |
| 8 | results.push(...page.data); |
| 9 | cursor = page.nextCursor; |
| 10 | } catch (error) { |
| 11 | logger.error('Error fetching page', error); |
| 12 | break; // silently stops; caller gets partial results as if complete |
| 13 | } |
| 14 | } while (cursor); |
| 15 |
|
| 16 | return results; |
| 17 | } |
Explanation (EN)
Catching, logging, and breaking turns a fetch failure into a truncated-but-successful-looking result. The caller cannot tell the data is incomplete.
Objašnjenje (HR)
Hvatanje, logiranje i prekid pretvaraju pogresku dohvata u rezultat koji izgleda uspjesno, ali je nepotpun. Pozivatelj ne moze znati da podaci nisu cjeloviti.
Good example
| 1 | async function fetchAll(params: Params): Promise<Item[]> { |
| 2 | const results: Item[] = []; |
| 3 | let cursor: string | undefined; |
| 4 |
|
| 5 | do { |
| 6 | const page = await api.getPage({ ...params, cursor }); |
| 7 | results.push(...page.data); |
| 8 | cursor = page.nextCursor; |
| 9 | } while (cursor); |
| 10 |
|
| 11 | return results; |
| 12 | } |
| 13 | // Let the error propagate; the caller decides whether to retry, fail, or degrade. |
Explanation (EN)
Letting the error propagate gives the caller a true failure signal so it can retry, fail loudly, or fall back deliberately instead of acting on partial data.
Objašnjenje (HR)
Propustanjem pogreske prema gore pozivatelj dobiva stvarni signal o gresci pa moze ponoviti, glasno pasti ili svjesno degradirati umjesto da radi s nepotpunim podacima.
Exceptions / Tradeoffs (EN)
If partial results are explicitly acceptable, document it and signal the truncation to the caller (e.g. return a `{ data, complete: false }` shape) rather than swallowing it.
Iznimke / Tradeoffi (HR)
Ako su djelomicni rezultati izricito prihvatljivi, dokumentirajte to i signalizirajte rezanje pozivatelju (npr. vratite oblik `{ data, complete: false }`) umjesto da pogresku progutate.