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
api-designerror-handlinghttpseparation-of-concerns
Let services own failure detail and return a value-or-null, not transport codes
A service method should return the thing (or null) and handle its own failures; callers shouldn't translate internal failure modes into arbitrary HTTP status codes.
PR: hegnar-web · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | const r = await service.getToken(); |
| 2 | if (r.reason === 'badCountry') return res.status(500).end(); |
| 3 | if (r.reason === 'upstream') return res.status(502).end(); |
| 4 | if (r.reason === 'noData') return res.status(422).end(); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | const token = await service.getToken(); // logs+handles its own failures, returns string | null |
| 2 | if (!token) return res.status(500).json({ error: 'Could not resolve token' }); |
| 3 | return res.json({ token }); |
Explanation (EN)
Objašnjenje (HR)