Gestion des erreurs 404 et 500
Actuellement, les erreurs 404 et 500 retournent du HTML, ce qui n'est pas très approprié à une API REST/JSON. L'idée est donc d'ajouter un catcher d'erreurs à la fin de la chaîne de traitement afin de retourner du JSON :
Erreur 404 :
http GET http://127.0.0.1:3000/crater/api/unknown
HTTP/1.1 404 Not Found
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 82
Content-Type: application/json; charset=utf-8
Date: Fri, 19 Feb 2021 09:50:56 GMT
ETag: W/"52-jmlC0Hl0cghWnBw4K/xOqQ8vcD4"
Keep-Alive: timeout=5
X-Powered-By: Express
{
"description": "unknown path: /crater/api/unknown",
"message": "Resource Not Found"
}
Erreur 500 :
http GET http://127.0.0.1:3000/erreur python3-1 | 0 < 09:43:01
HTTP/1.1 500 Internal Server Error
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 56
Content-Type: application/json; charset=utf-8
Date: Fri, 19 Feb 2021 08:46:34 GMT
ETag: W/"38-xPjrj7lrSXqoVuV2bQWFOKmZwaY"
Keep-Alive: timeout=5
X-Powered-By: Express
{
"description": "oops",
"message": "Internal Server Error"
}
Edited by Boris Jamot