Django — Catch request data read error

Every time an upload fails, I get a 500 email from django and it’s getting old.

I’ve looked into this a few times but it appears it’s a tough problem to “solve” because there is no standardized error message for WSGI connection breaks such as this.

I do know that mod_python throws a different exception message, and you definitely can’t just capture all IOErrors.

I understand this is platform dependent, but I’ve set up an exception catching middleware that checks for ‘request data read error’ in the exception message and swallows it if found.

class CatchUploadIOErrorMiddleware(object):
    def process_exception(self, request, exception):
        msg = exception.message
        if 'request data read error' in msg:
            log.warn("Catching IOError.. {msg}".format(msg=msg))
            response = http.HttpResponse('[insert useful error here]') 
            response.status_code = 500
            return response

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s