Code

Opened 4 years ago

Closed 3 years ago

#13850 closed (invalid)

Make handle_uncaught_exception() more easily patch-able

Reported by: leonidg Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: mail, unhandlederror, http500
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When a 500 error happens that is not handled by any middleware, etc., Django goes into core.handlers.base.handle_uncaught_exception, which, if settings.DEBUG is set to False, generates a simple email that contains the traceback and request data and sends it to the admins specified in settings.py. This behavior is not configurable, and in particular there is no easy way to customize what the contents of the email is. The best one can hope to do is to subclass or monkey-patch BaseHandler.

It would be nice for this function to use a helper method of BaseHandler that takes the same information (request and exc_info) and returns the (subject, message) tuple. Then, applications that would like more detailed error emails can simply replace this function with whatever they want. The attached patch does this.

Attachments (1)

uncaught-exception-email.patch (1.6 KB) - added by leonidg 4 years ago.

Download all attachments as: .zip

Change History (4)

Changed 4 years ago by leonidg

comment:1 follow-up: Changed 4 years ago by berto

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Once this patch is applied where do you customize the make_uncaught_exception_email() function?

comment:2 in reply to: ↑ 1 Changed 4 years ago by leonidg

Replying to berto:

Once this patch is applied where do you customize the make_uncaught_exception_email() function?

You redefine it in settings.py. It's ugly, but it's strictly better than what one would have to do now, which is to redefine the entire handle_uncaught_exception() function.

comment:3 Changed 3 years ago by brodie

  • Resolution set to invalid
  • Status changed from new to closed

Django 1.3's logging framework allows you to configure what's invoked when there's an uncaught exception. E.g.,

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'foo.FooLogHandler'
        }
    },
    'loggers': {
        'django.request':{
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

Where foo.FooLogHandler is your own handler that fires off an email in the format you prefer.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.