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).
Route to a fallback data source via a capability check, not try/catch
Pick between a primary and fallback source with an explicit isAvailable() guard plus a domain condition, each branch its own method — don't rely on try/catch retry.
Bad example
| 1 | async fetchPriceHistory(token, query) { |
| 2 | try { |
| 3 | // assume primary works; if it throws, fall through |
| 4 | return await this.primary.fetch(query); |
| 5 | } catch (e) { |
| 6 | // swallows genuine bugs (timeouts, mapping errors) as 'unavailable' |
| 7 | return this.fallback.fetch(token, query); |
| 8 | } |
| 9 | } |
Explanation (EN)
A try/catch fallback cannot distinguish 'primary is offline' from 'primary threw a real bug', so it silently masks failures, retries expensive work, and is hard to test deterministically.
Objašnjenje (HR)
Fallback kroz try/catch ne razlikuje 'primarni izvor je nedostupan' od 'primarni je bacio stvarnu grešku', pa tiho prikriva kvarove, ponavlja skupe operacije i teško se determin:isticki testira.
Good example
| 1 | async fetchPriceHistory(token, query) { |
| 2 | const period = query.period.toUpperCase(); |
| 3 | const isIntraday = period === '1D' || period === '5D'; |
| 4 |
|
| 5 | // primary only handles non-intraday and only when it is up |
| 6 | if (!isIntraday && this.primary.isAvailable()) { |
| 7 | return this.fetchFromPrimary(query, period); |
| 8 | } |
| 9 | return this.fetchFromFallback(token, query, period); |
| 10 | } |
Explanation (EN)
An explicit capability check plus the domain condition makes routing intent obvious and deterministic; each branch is a separate private method, and real errors from the chosen source propagate instead of being caught.
Objašnjenje (HR)
Eksplicitna provjera dostupnosti uz domensku provjeru cini usmjeravanje jasnim i determin:istickim; svaka grana je zasebna privatna metoda, a stvarne greske odabranog izvora se propagiraju umjesto da budu uhvacene.
Exceptions / Tradeoffs (EN)
If the primary source has no cheap health/capability signal and the only way to know it failed is to call it, a guarded try/catch that re-throws non-availability errors is acceptable.
Iznimke / Tradeoffi (HR)
Ako primarni izvor nema jeftin signal dostupnosti i jedini nacin da se sazna kvar je poziv, prihvatljiv je try/catch koji ponovno baca greske koje nisu vezane uz dostupnost.