Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch error text in custom 500 handler

Tags:

django

Somewhere in my views, I throw an error, particularly this one:

views.py

from xmlrpclib import Fault

def some_function(request):
    if ....:
        return Fault(-1, 'foo')

Then, also in views.py, I have my custom 500 handler to catch server errors:

def my_custom_500(request):
    context = {...}
    ### Here is where I need to catch `'foo'` 
    ### in order to put it in the context and pass it to the template
    render(request, '500.html', context)

Is there anyway in which I can access the error message? Thanks

like image 294
Mihai Zamfir Avatar asked Jul 01 '14 13:07

Mihai Zamfir


1 Answers

Try to override django.conf.urls.defaults.handler500 in your urls.py.

from django.conf.urls.defaults import *
handler500 = 'path.to.my_custom_500'

or even better - write your own handler and put it in the LOGGING settings.

Edit:

You can also add to your my_custom_500 code that will recognize type of exception, eg:

import sys; 

def my_custom_500(request):
    ...
    type_, value, traceback = sys.exc_info()
    ...
like image 129
pancakes Avatar answered Nov 12 '22 21:11

pancakes