Custom 500 error message should only be seen for Preview
This commit is contained in:
@@ -56,13 +56,18 @@ def jsonable_server_error(request, template_name='500.html'):
|
||||
return server_error(request, template_name=template_name)
|
||||
|
||||
|
||||
def handle_500(template_path, context=None):
|
||||
def handle_500(template_path, context=None, test_func=None):
|
||||
"""
|
||||
Decorator for view specific 500 error handling.
|
||||
Custom handling will be skipped only if test_func is passed and it returns False
|
||||
|
||||
Usage::
|
||||
Usage:
|
||||
|
||||
@handle_500(template_path='certificates/server-error.html', context={'error-info': 'Internal Server Error'})
|
||||
@handle_500(
|
||||
template_path='certificates/server-error.html',
|
||||
context={'error-info': 'Internal Server Error'},
|
||||
test_func=lambda request: request.GET.get('preview', None)
|
||||
)
|
||||
def my_view(request):
|
||||
# Any unhandled exception in this view would be handled by the handle_500 decorator
|
||||
# ...
|
||||
@@ -83,9 +88,15 @@ def handle_500(template_path, context=None):
|
||||
if settings.DEBUG:
|
||||
# In debug mode let django process the 500 errors and display debug info for the developer
|
||||
raise
|
||||
else:
|
||||
elif test_func is None or test_func(request):
|
||||
# Display custom 500 page if either
|
||||
# 1. test_func is None (meaning nothing to test)
|
||||
# 2. or test_func(request) returns True
|
||||
log.exception("Error in django view.")
|
||||
return render_to_response(template_path, context)
|
||||
else:
|
||||
# Do not show custom 500 error when test fails
|
||||
raise
|
||||
return inner
|
||||
return decorator
|
||||
|
||||
|
||||
Reference in New Issue
Block a user