Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
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.
Bad example
| 1 | try { |
| 2 | return await this.repo.create(post); |
| 3 | } catch { |
| 4 | throw new ConflictException(); // any failure becomes 409 |
| 5 | } |
Explanation (EN)
A blanket mapping turns connection, permission or schema failures into a misleading conflict.
Objašnjenje (HR)
Good example
| 1 | try { |
| 2 | return await repo.create(post); |
| 3 | } catch (error) { |
| 4 | if (isPostIdentityConflict(error)) throw new ConflictException("Post already exists"); |
| 5 | throw error; |
| 6 | } |
Explanation (EN)
The predicate checks the actual driver code and expected unique constraint; other failures preserve their original classification.
Objašnjenje (HR)
Notes (EN)
Classify known database errors by documented driver code and constraint identity, mapping only the intended conflict while preserving unrelated failures.
Exceptions / Tradeoffs (EN)
A preflight lookup is not race-safe conflict prevention. Keep a database uniqueness constraint and handle concurrent conflicts at the write.