Why does Django Http404 return a JSON payload while all other Http error responses don't?
Django's http response.py library provides an Http404 class that can be used to raise a "not found" exception which returns a 404 along with the following JSON payload: {"detail":"Not found."}. However there are no other HTTP error response classes that do this. All the other standard error classes don't (such as HttpResponseBadRequest, HttpResponseServerError, HttpResponseBadRequest, etc).
What is the rationale behind only providing a single error class (for a 404) that returns a JSON payload?
As far as I understand, this is less of a Django quirk and more of a 404 error quirk. 404 errors are slightly different from other exceptions in that they are routing outcomes, so no endpoint is actually handling this error, as can be assumed with other HTTP error types. This can be seen in practice in Django in the urls app in resolvers.py:
class URLResolver:
def resolve(self, path):
...
if match:
...
raise Resolver404({"path": path})
Other HTTP errors can only be raised by an endpoint if it encounters an error, so they have all of the endpoint's existing error handling infrastructure to cleanly handle it without crashing. However, 404 errors literally mean that there is no endpoint to manage this request, and so the error itself is responsible for returning a response (where other endpoints have code for this behaviour), and in DRF this default response is {"detail":"Not found."}.
Hope this explanation helps
This suggests that 404 explicitly means "endpoint not found". But based on the definition it is much more broad than that and can absolutely mean "resource not found" which is a valid error that could be encountered by an existing endpoint.
Ultimately this JSON payload implies that a variety of HTTP errors should also provide a similar JSON schema payload with a detail field. Otherwise the front-end has to handle the existence of a JSON payload with a detail field for only one case and that feels pretty weird.