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).
Don't let a swallowed upstream failure masquerade as a cacheable success response
If a data-fetching layer catches its own errors and returns an empty/default value instead of throwing, that default is indistinguishable from a genuinely empty result at the response layer — and if the response gets a Cache-Control TTL, one transient upstream blip gets pinned at the edge for the whole TTL, serving wrong data to everyone until it expires.
Bad example
| 1 | async function fetchArticles() { |
| 2 | try { |
| 3 | return await es.search(...); |
| 4 | } catch { |
| 5 | return { articles: [], total: 0 }; // swallowed |
| 6 | } |
| 7 | } |
| 8 |
|
| 9 | // route handler |
| 10 | const result = await fetchArticles(); |
| 11 | res.header('Cache-Control', 's-maxage=60').json(result); |
Explanation (EN)
There is no way to tell a real empty result from a failed search once the error is swallowed, so a single ES blip gets cached as truth for 60 seconds.
Objašnjenje (HR)
Nakon što je greška progutana, nema načina razlikovati stvaran prazan rezultat od neuspjele pretrage, pa jedan kratki ES kvar biva keširan kao istina 60 sekundi.
Good example
| 1 | async function fetchArticles() { |
| 2 | const result = await es.search(...); // let it throw |
| 3 | return result; |
| 4 | } |
| 5 |
|
| 6 | // route handler |
| 7 | try { |
| 8 | const result = await fetchArticles(); |
| 9 | res.header('Cache-Control', 's-maxage=60').json(result); |
| 10 | } catch { |
| 11 | res.status(503).header('Cache-Control', 'no-store').json({ error: 'unavailable' }); |
| 12 | } |
Explanation (EN)
Letting the failure propagate lets the route distinguish a real empty result (cacheable) from an upstream error (must not be cached), so a blip only affects the requests during the blip itself.
Objašnjenje (HR)
Propuštanjem greške da se propagira, ruta može razlikovati stvaran prazan rezultat (kešira se) od greške u pozadinskom sustavu (ne smije se kešem), pa kratki kvar utječe samo na zahtjeve tijekom samog kvara.