More data (model) in Http404 exception

Hi everyone ! I was thinking about a little something and wanted to discuss its relevance as the creation of a ticket.

The Http404 exception is a basic exception inheriting from Exception and nothing else :

class Http404(Exception):

I have some pages with multiple get_object_or_404 and no straight forward way to discriminate 404s based on what caused the error.

I wanted to propose the Http404 exception to carry more data, at least the model, when raised. I think it would be relevant for usages such as get_object_or_404 or Paginator.paginate_queryset.

Once the rich error is raised, besides the handler having more data to so stuff with, I can also easily improve my Sentry’s fingerprinting and monitor more closely what is happing on my application.

Would such a feature be useful to you ? What data besides the model would you have usage for ?
Thank you

Http404 doesn’t just apply to get_object_or_404 though. A “model” attribute on the exception would thus only be used in certain cases, and it would cut through the various layers of Django.

The exception message already includes the name of the model that is missing. That is normally useful enough. I would suggest that if you need more data, you write your own version of get_object_or_404 that uses a subclass of Http404 with extra attributes.

For monitoring/error applications you can also use stack inspection to pull out useful data.