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).
Do not return a success status when the response body may be null
Validate the result of an upstream call before sending a 2xx; a null/empty body with a 200/201 hides failures from the client.
Bad example
| 1 | const handler = async (req, res) => { |
| 2 | const result = await service.addItem(payload); |
| 3 | // Sends 201 even when result is null (upstream no-op or failure) |
| 4 | return res.status(201).json(result); |
| 5 | }; |
Explanation (EN)
The handler always responds with a success status, even if the upstream service returned null. The client receives 201 with an empty body and cannot tell the operation actually failed.
Objašnjenje (HR)
Handler uvijek vraca uspjesan status, cak i kad je servis vratio null. Klijent dobije 201 s praznim tijelom i ne moze prepoznati da operacija zapravo nije uspjela.
Good example
| 1 | const handler = async (req, res) => { |
| 2 | const result = await service.addItem(payload); |
| 3 | if (!result) { |
| 4 | return res.status(500).json({ message: 'Failed to add item' }); |
| 5 | } |
| 6 | return res.status(200).json(result); |
| 7 | }; |
Explanation (EN)
The result is checked before responding. A null result maps to an error status, so the success code is only sent when there is a real payload.
Objašnjenje (HR)
Rezultat se provjerava prije odgovora. Null rezultat se mapira na error status, pa se uspjesni kod salje samo kad stvarno postoji sadrzaj.
Exceptions / Tradeoffs (EN)
Endpoints that legitimately return no content can use 204 No Content with an empty body.
Iznimke / Tradeoffi (HR)
Endpointi koji legitimno ne vracaju sadrzaj mogu koristiti 204 No Content s praznim tijelom.