diff --git a/cms/djangoapps/contentstore/views/preview.py b/cms/djangoapps/contentstore/views/preview.py index eb2d92880d..22a0af8171 100644 --- a/cms/djangoapps/contentstore/views/preview.py +++ b/cms/djangoapps/contentstore/views/preview.py @@ -6,7 +6,7 @@ from django.conf import settings from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseForbidden from django.core.urlresolvers import reverse from django.contrib.auth.decorators import login_required -from mitxmako.shortcuts import render_to_response +from mitxmako.shortcuts import render_to_response, render_to_string from xmodule_modifiers import replace_static_urls, wrap_xmodule from xmodule.error_module import ErrorDescriptor @@ -79,9 +79,18 @@ def preview_component(request, location): # can bind to it correctly component.runtime.wrappers.append(partial(wrap_xmodule, 'xmodule_edit.html')) + + try: + content = component.render('studio_view').content + # catch exceptions indiscriminately, since after this point they escape the + # dungeon and surface as uneditable, unsaveable, and undeletable + # component-goblins. + except Exception as exc: #pylint: disable=W0703 + content = render_to_string('html_error.html', {'message': str(exc)}) + return render_to_response('component.html', { 'preview': get_preview_html(request, component, 0), - 'editor': component.render('studio_view').content, + 'editor': content }) @@ -157,4 +166,8 @@ def get_preview_html(request, descriptor, idx): specified by the descriptor and idx. """ module = load_preview_module(request, str(idx), descriptor) - return module.render("student_view").content + try: + content = module.render("student_view").content + except Exception as exc: #pylint: disable=W0703 + content = render_to_string('html_error.html', {'message': str(exc)}) + return content diff --git a/cms/templates/html_error.html b/cms/templates/html_error.html new file mode 100644 index 0000000000..7bc75ad0a9 --- /dev/null +++ b/cms/templates/html_error.html @@ -0,0 +1,21 @@ +<%! from django.utils.translation import ugettext as _ %> +<%! from django.core.urlresolvers import reverse %> +<%block name="bodyclass">error + +<%block name="content"> +
+

+ ${_("We're having trouble rendering your component.")} +

+ % if message: +

+ ${_("Error message:")} +

+ +
+      ${message | h}
+      
+
+ % endif +
+