Commit c068bc95 by Saleem Latif

Custom 500 error message should only be seen for Preview

parent a8cca47c
...@@ -56,13 +56,18 @@ def jsonable_server_error(request, template_name='500.html'): ...@@ -56,13 +56,18 @@ def jsonable_server_error(request, template_name='500.html'):
return server_error(request, template_name=template_name) 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. 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): def my_view(request):
# Any unhandled exception in this view would be handled by the handle_500 decorator # 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): ...@@ -83,9 +88,15 @@ def handle_500(template_path, context=None):
if settings.DEBUG: if settings.DEBUG:
# In debug mode let django process the 500 errors and display debug info for the developer # In debug mode let django process the 500 errors and display debug info for the developer
raise 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.") log.exception("Error in django view.")
return render_to_response(template_path, context) return render_to_response(template_path, context)
else:
# Do not show custom 500 error when test fails
raise
return inner return inner
return decorator return decorator
......
...@@ -454,9 +454,13 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): ...@@ -454,9 +454,13 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase):
user_id=self.user.id, user_id=self.user.id,
course_id=unicode(self.course.id) course_id=unicode(self.course.id)
) )
response = self.client.get(test_url) response = self.client.get(test_url + "?preview=honor")
self.assertIn("Invalid Certificate Configuration", response.content) self.assertIn("Invalid Certificate Configuration", response.content)
# Verify that Exception is raised when certificate is not in the preview mode
with self.assertRaises(Exception):
self.client.get(test_url)
@override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED)
def test_certificate_evidence_event_emitted(self): def test_certificate_evidence_event_emitted(self):
self.client.logout() self.client.logout()
......
...@@ -281,7 +281,10 @@ def _update_certificate_context(context, course, user, user_certificate): ...@@ -281,7 +281,10 @@ def _update_certificate_context(context, course, user, user_certificate):
) )
@handle_500(template_path="certificates/server-error.html") @handle_500(
template_path="certificates/server-error.html",
test_func=lambda request: request.GET.get('preview', None)
)
def render_html_view(request, user_id, course_id): def render_html_view(request, user_id, course_id):
""" """
This public view generates an HTML representation of the specified student's certificate This public view generates an HTML representation of the specified student's certificate
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment