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).
Do not block or risk crashing startup inside init lifecycle hooks
Run heavy/fallible startup work fire-and-forget with its own catch, so it does not delay listen() or crash the process.
Bad example
| 1 | async onModuleInit(): Promise<void> { |
| 2 | // Awaited before app.listen(): delays readiness; a throw aborts bootstrap |
| 3 | await this.populateFromUpstream(); |
| 4 | } |
Explanation (EN)
The framework awaits onModuleInit before the HTTP server listens, so a long or throwing populate blocks readiness probes and can crash the pod into a restart loop.
Objašnjenje (HR)
Framework čeka onModuleInit prije nego što HTTP server počne slušati, pa dugo izvođenje ili bacanje greške blokira readiness probe i može srušiti pod u petlju ponovnog pokretanja.
Good example
| 1 | onModuleInit(): void { |
| 2 | // Fire-and-forget so bootstrap completes regardless of outcome |
| 3 | void this.populateFromUpstream().catch((err) => { |
| 4 | this.logger.error('Startup populate failed', err); |
| 5 | }); |
| 6 | } |
Explanation (EN)
Detaching the work and attaching a catch lets the server start listening immediately, and any failure is logged instead of aborting bootstrap.
Objašnjenje (HR)
Odvajanje posla i dodavanje catch-a omogućuje serveru da odmah počne slušati, a svaki kvar se logira umjesto da prekine pokretanje.
Exceptions / Tradeoffs (EN)
Work that is a genuine precondition for serving traffic (e.g. running DB migrations) should stay awaited, but then it must have explicit error handling and bounded duration.
Iznimke / Tradeoffi (HR)
Posao koji je stvarni preduvjet za posluživanje prometa (npr. pokretanje DB migracija) treba ostati awaitan, ali tada mora imati eksplicitno rukovanje greškama i ograničeno trajanje.