From f82d61e6948ada7331b1787a17dd5d9a7d9e176c Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Wed, 5 Apr 2017 15:43:30 +0200 Subject: [PATCH] add error template on cell renderer (#15768) --- combo/data/models.py | 8 +++- .../templates/combo/cell-template-error.html | 10 +++++ tests/test_cells.py | 48 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 combo/public/templates/combo/cell-template-error.html diff --git a/combo/data/models.py b/combo/data/models.py index 3382597..154d6fe 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -462,7 +462,13 @@ class CellBase(models.Model): if self.slug: template_names.append('combo/cells/%s/%s' % (self.slug, base_template_name)) template_names.reverse() - tmpl = template.loader.select_template(template_names) + try: + tmpl = template.loader.select_template(template_names) + except (template.TemplateDoesNotExist, template.TemplateSyntaxError) as exception: + tmpl = template.loader.get_template('combo/cell-template-error.html') + context['exception'] = exception + context['exception_type'] = exception.__class__.__name__ + context['template_names'] = template_names return tmpl.render(context) def modify_global_context(self, context, request=None): diff --git a/combo/public/templates/combo/cell-template-error.html b/combo/public/templates/combo/cell-template-error.html new file mode 100644 index 0000000..fbf074a --- /dev/null +++ b/combo/public/templates/combo/cell-template-error.html @@ -0,0 +1,10 @@ +{% load i18n %} +{% trans "template error:" %} {{ exception_type }} + +{% if debug %} +
+{% trans "cell:" %} {{ cell }}
+{% trans "template names:" %} {{ template_names }}
+{% trans "exception message:" %} {{ exception }}
+
+{% endif %} diff --git a/tests/test_cells.py b/tests/test_cells.py index 3b15fd9..07edf97 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -10,6 +10,7 @@ from django.template import Context from django.test import override_settings from django.test.client import RequestFactory from django.contrib.auth.models import User +from django.core.urlresolvers import reverse from combo.utils import NothingInCacheException @@ -231,3 +232,50 @@ def test_config_json_cell(): context = cell.get_cell_extra_context(Context({'request': request})) assert context['json'] == {'hello': 'world'} assert context['parameters'] == {'blah': 'plop'} + +def test_cell_template_error(app): + Page.objects.all().delete() + page = Page(title='example page', slug='example-page') + page.save() + + with override_settings(JSON_CELL_TYPES={'foobar': {'name': 'Foobar', 'url': 'http://test/'}, + 'badsyntax': {'name': 'Error', 'url': 'http://test/'}}, + TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]): + # missing template + cell = ConfigJsonCell() + cell.key = 'foobar' + cell.page = page + cell.order = 0 + cell.save() + + url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, + 'cell_reference': cell.get_reference()}) + data = {'data': []} + + with mock.patch('combo.utils.requests.get') as requests_get: + requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + with override_settings(DEBUG=False): + resp_body = app.get(url).body.strip() + assert resp_body == 'template error: TemplateDoesNotExist' + with override_settings(DEBUG=True): + resp_body = app.get(url).body.strip() + assert resp_body.startswith('template error: TemplateDoesNotExist') + assert 'cell: config json cell' in resp_body + assert 'template names: [' in resp_body + assert 'exception message: combo/json/foobar.html, combo/configjsoncell.html' in resp_body + + # syntax error (see badsyntax.html) + cell.key = 'badsyntax' + cell.save() + with mock.patch('combo.utils.requests.get') as requests_get: + requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + with override_settings(DEBUG=False): + resp_body = app.get(url).body.strip() + assert resp_body == 'template error: TemplateSyntaxError' + with override_settings(DEBUG=True): + resp_body = app.get(url).body.strip() + assert resp_body.startswith('template error: TemplateSyntaxError') + assert 'cell: config json cell' in resp_body + assert 'template names: [' in resp_body + assert 'exception message: Invalid block tag' in resp_body + assert 'foo_boom_bar' in resp_body -- 2.11.0