Una buona risposta di errore fa due cose: usa il codice di stato HTTP corretto (il segnale leggibile dalla macchina), e restituisce un body strutturato e coerente su cui uno sviluppatore può agire. Forme di errore incoerenti sono una delle maggiori fonti di dolore lato client.
Usa un formato di errore standard: RFC 7807 (problem+json)
RFC 7807 definisce un body "problem details" standard con un media type dedicato, così ogni errore nella tua API appare uguale:
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{
"type": "https://api.example.com/errors/not-found",
"title": "Resource not found",
"status": 404,
"detail": "No user exists with id 999",
"instance": "/users/999"
}
