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).
Guard external service calls with error handling and logging
Wrap API/datastore calls in try-catch, log with context, and return a safe fallback or rethrow a meaningful error.
Bad example
| 1 | async getInstruments(ids: number[]) { |
| 2 | return this.searchProvider.byIds(ids).search(); |
| 3 | } |
Explanation (EN)
A connection failure throws an unhandled error with no context, surfacing as a generic 500 with nothing useful in the logs.
Objašnjenje (HR)
Pad veze baca neuhvaćenu pogrešku bez konteksta, koja izlazi kao generički 500 bez ičeg korisnog u logovima.
Good example
| 1 | async getInstruments(ids: number[]) { |
| 2 | try { |
| 3 | return await this.searchProvider.byIds(ids).search(); |
| 4 | } catch (error) { |
| 5 | Logger.error(`Failed to fetch instruments by ids: ${error.message}`, error.stack); |
| 6 | throw error; |
| 7 | } |
| 8 | } |
Explanation (EN)
The failure is logged with context, and the caller gets a deliberate behaviour (rethrow here, or a safe fallback where appropriate).
Objašnjenje (HR)
Pad je zabilježen s kontekstom, a pozivatelj dobiva namjerno ponašanje (ovdje rethrow, ili siguran fallback gdje je prikladno).
Notes (EN)
Choose fallback vs rethrow per endpoint: returning an empty result can hide outages, so prefer rethrow unless a degraded response is genuinely acceptable. A global error interceptor can cover this instead of per-method try-catch.
Bilješke (HR)
Biraj fallback ili rethrow po endpointu: vraćanje praznog rezultata može sakriti ispade, pa preferiraj rethrow osim ako je degradirani odgovor zaista prihvatljiv. Globalni error interceptor može ovo pokriti umjesto try-catcha po metodi.
Exceptions / Tradeoffs (EN)
Balance against do-not-silently-default-on-error-for-critical-values: catch and return a safe fallback only for non-critical paths where a default is semantically valid.