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).
Keep error response content-type consistent with the success path on the same endpoint
If an endpoint's success response is JSON, make sure every error branch on that same endpoint returns JSON too — don't let a thrown error fall through to a generic error handler that sends plain text/HTML, since a client that unconditionally JSON.parses every response from that endpoint will crash on the error case.
Bad example
| 1 | // success path |
| 2 | res.json({ articles }); |
| 3 |
|
| 4 | // error path — falls through to a generic handler |
| 5 | if (!author) throw new Http404(); // errorHandler does res.status(404).send('Not Found') |
Explanation (EN)
The success path is JSON but the error path becomes text/html — a client that always calls response.json() throws on the 404 case instead of getting a handled error.
Objašnjenje (HR)
Uspješan put je JSON, ali greška postaje text/html — klijent koji uvijek poziva response.json() dobit će iznimku na 404 slučaju umjesto obrađene greške.
Good example
| 1 | // success path |
| 2 | res.json({ articles }); |
| 3 |
|
| 4 | // error path — explicit JSON, matching content-type |
| 5 | if (!author) { |
| 6 | return res.status(404).json({ error: 'Not Found', message: 'Author not found' }); |
| 7 | } |
Explanation (EN)
Both branches return the same content-type, so any client parsing this endpoint's responses can do so unconditionally.
Objašnjenje (HR)
Obje grane vraćaju isti content-type, pa svaki klijent koji parsira odgovore ovog endpointa to može raditi bezuvjetno.