Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: node
error-handlingexceptionsapi-designcorrectness
Throw a precise error for the actual condition instead of blanket-mapping every failure
Detect the specific failure (e.g. a duplicate via a lookup) and throw the matching exception, rather than catching any DB error and always returning the same status, which misleads the client.
PR: hegnar-investor-ws · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | try { |
| 2 | return await this.repo.create(post); |
| 3 | } catch { |
| 4 | throw new ConflictException(); // any failure becomes 409 |
| 5 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | if (await this.repo.findOne({ where: key })) { |
| 2 | throw new ConflictException('Post already exists'); |
| 3 | } |
| 4 | return this.repo.create(post); |
Explanation (EN)
Objašnjenje (HR)