Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Define failure policy for initialization and cleanup
Handle lifecycle failures according to readiness and cleanup contracts; symmetry alone does not justify swallowing errors.
Bad example
| 1 | async onModuleInit() { |
| 2 | try { |
| 3 | await this.client.init(); |
| 4 | } catch (err) { |
| 5 | this.logger.warn(`Init failed: ${(err as Error).message}`); |
| 6 | } |
| 7 | } |
| 8 |
|
| 9 | async onModuleDestroy() { |
| 10 | await this.client.quit(); // unguarded — a rejection here aborts remaining teardown |
| 11 | } |
Explanation (EN)
An unhandled cleanup rejection can interrupt the remaining shutdown sequence.
Objašnjenje (HR)
Neobrađena pogreška cleanupa može prekinuti ostatak postupka gašenja.
Good example
| 1 | async onModuleDestroy() { |
| 2 | try { |
| 3 | await this.client.quit(); |
| 4 | } catch (err) { |
| 5 | this.logger.warn(`Cleanup failed: ${(err as Error).message}`); |
| 6 | } |
| 7 | } |
Explanation (EN)
Apply the same defensive error handling to every lifecycle hook that performs I/O, so a failure in one teardown step doesn't prevent the rest of the shutdown sequence from running.
Objašnjenje (HR)
Primijeni istu defenzivnu obradu grešaka na svaki lifecycle hook koji radi I/O, tako da greška u jednom koraku gašenja ne spriječi izvršavanje ostatka shutdown sekvence.
Notes (EN)
Define failure policy independently for initialization and teardown, including partial initialization and bounded shutdown time.
Exceptions / Tradeoffs (EN)
Matching try/catch syntax is not a goal. Required initialization may need to fail readiness, while cleanup may continue releasing other resources and report the first failure.