Allow raising exceptions to be handled gracefully

Raising Http404 logs the response as if HttpResponseNotFound was returned, but raising BadRequest includes exc_info and that differs from a response of HttpResponseBadRequest and looks like something exceptional went wrong in the logs. I’d like to be able to disable that, with something like NO_LOG_EXCEPTION_IN_RESPONSE to allow the raise handling to be a first class citizen like return handling. This would simplify error handling in code because then return can be used for the flow of successful responses (2XX/3XX) and raise is for errors (4XX).
I submitted a PR to show one potentially implementation of this and I’ve been patching it into my code and using it successfully.
Any feedback on this?

1 Like

Hi Dave. Would this be something you might be able to configure via the logging? I realize you’re proposing a feature, but if there’s a different configuration option available that doesn’t require a code change, we may want to go that route.

Definitely open to other options, but I’m not sure how this could be configured via logging. Do you have a recommended solution for that?

Ah, my understanding of python logging isn’t as great as I had hoped. It appears you’d have to write a filter class to manipulate the logging record, then update your logging configuration to use that filter.

This can be achieved by using a custom log-filter/handler which removes the stacktrace and exception info from the log before it is emitted see this answer on SO.

1 Like