<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1870732089876948&amp;ev=PageView&amp;noscript=1">

Add Proactive Error Reporting to Your Django App

No application development project is complete without a dedicated testing phase to ensure that the code is bug-free and that the app runs smoothly. But what if you forget a use case, or don’t take take every possible angle towards testing a specific feature? Your code may produce an error and return a very unhelpful HTTP 500 page to your users.

Unhelpful HTTP 500 Error Page | Cloudbakers

It’s important to notify your support team that the issue occurred, so that they can begin working on a resolution before the user even has a chance to submit their own report. In only a couple of steps, you can add an email notification to your Django app that will alert you in the event that your application returns a nasty 500 error page. Let's walk through it!

Step 1: Override the Default 500 Error View

Override Django’s default 500 error handler by adding the following line of code to the bottom of your urls.py file. This defines a custom view that will execute whenever a 500 error is encountered.

handler500 = 'portal.views.custom_500'

Step 2: Implement the Custom 500 View

Now implement the view that we just referenced in the url configuration file. The code below sends a simple email notification to alert the application’s support team that something has gone wrong. Then, just like Django’s default 500 error handler, the view returns the 500 template to display to the user.

from django.core.mail import EmailMessage

def custom_500(request):
    if IN_PROD:
        msg = EmailMessage(
            sender='admin@your-app.com',
            to='support@dev-company.com',
            subject='500 ERROR ALERT',
            html='A 500 error has occurred. Check your app’s logs for more information.'
        )
        msg.send()
    return render_to_response("500.html", {}, context_instance=RequestContext(request))

You can add any relevant information to the body of the alert email, like a traceback, timestamp, or the name of the user that experienced the issue. If your application is running on Google App Engine, then you may prefer to just include a link to the project’s logs in the Developer Console. Your support staff will then be able to reference the wealth of information there and begin troubleshooting the issue only seconds after it occurred.

Real time error reporting like this will allow your team to more quickly troubleshoot and resolve issues - hopefully before your users even have a chance to report them!

New Call-to-action

Originally published on January 28, 2016

If you enjoyed this post, please consider sharing

Cloud colaboration
New Call-to-action

Subscribe to
Crumbs From the Cloud

Topics

Posts by Topic

see all