cells: jsoncell log error on syntax error only (#57626)
This commit is contained in:
parent
e1dcad5831
commit
e415373110
|
@ -1852,7 +1852,7 @@ class JsonCellBase(CellBase):
|
||||||
url = utils.get_templated_url(data_url_dict['url'], context)
|
url = utils.get_templated_url(data_url_dict['url'], context)
|
||||||
except utils.TemplateError as e:
|
except utils.TemplateError as e:
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
if log_errors:
|
if log_errors and e.msg.startswith('syntax error'):
|
||||||
logger.error('error in templated URL (%s): %s', data_url_dict['url'], e)
|
logger.error('error in templated URL (%s): %s', data_url_dict['url'], e)
|
||||||
else:
|
else:
|
||||||
logger.debug('error in templated URL (%s): %s', data_url_dict['url'], e)
|
logger.debug('error in templated URL (%s): %s', data_url_dict['url'], e)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
@ -459,7 +460,8 @@ def mocked_request(*args, **kwargs):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_json_cell():
|
def test_json_cell(caplog):
|
||||||
|
caplog.set_level(logging.DEBUG)
|
||||||
page = Page(title='example page', slug='example-page')
|
page = Page(title='example page', slug='example-page')
|
||||||
page.save()
|
page.save()
|
||||||
|
|
||||||
|
@ -577,11 +579,19 @@ def test_json_cell():
|
||||||
assert requests_get.call_args[0][0] == 'http://testuser?barfoo'
|
assert requests_get.call_args[0][0] == 'http://testuser?barfoo'
|
||||||
assert requests_get.call_args[1]['cache_duration'] == 10
|
assert requests_get.call_args[1]['cache_duration'] == 10
|
||||||
assert requests_get.call_count == 1
|
assert requests_get.call_count == 1
|
||||||
|
caplog.clear()
|
||||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||||
context = cell.get_cell_extra_context({})
|
context = cell.get_cell_extra_context({})
|
||||||
# can't get URL, 'foobar' variable is missing
|
# can't get URL, 'foobar' variable is missing
|
||||||
assert context['json'] is None
|
assert context['json'] is None
|
||||||
assert requests_get.call_count == 0
|
assert requests_get.call_count == 0
|
||||||
|
assert caplog.record_tuples == [
|
||||||
|
(
|
||||||
|
'combo.data.models',
|
||||||
|
logging.DEBUG,
|
||||||
|
'error in templated URL (http://testuser?[foobar]): unknown variable foobar',
|
||||||
|
)
|
||||||
|
]
|
||||||
request = RequestFactory().get('/')
|
request = RequestFactory().get('/')
|
||||||
request.user = User(username='foo', email='foo@example.net')
|
request.user = User(username='foo', email='foo@example.net')
|
||||||
cell.url = 'http://testuser?email=[user_email]'
|
cell.url = 'http://testuser?email=[user_email]'
|
||||||
|
@ -594,6 +604,38 @@ def test_json_cell():
|
||||||
assert requests_get.call_args[0][0] == 'http://testuser?email=foo%40example.net'
|
assert requests_get.call_args[0][0] == 'http://testuser?email=foo%40example.net'
|
||||||
assert requests_get.call_args[1]['cache_duration'] == 10
|
assert requests_get.call_args[1]['cache_duration'] == 10
|
||||||
|
|
||||||
|
# URL is a template
|
||||||
|
cell.url = 'http://testuser?{{ concerned_user|name_id }}'
|
||||||
|
caplog.clear()
|
||||||
|
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||||
|
context = cell.get_cell_extra_context({})
|
||||||
|
# can't get URL, variable is missing
|
||||||
|
assert context['json'] is None
|
||||||
|
assert requests_get.call_count == 0
|
||||||
|
assert caplog.record_tuples == [
|
||||||
|
(
|
||||||
|
'combo.data.models',
|
||||||
|
logging.DEBUG,
|
||||||
|
'error in templated URL (http://testuser?{{ concerned_user|name_id }}): name_id',
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
cell.url = 'http://testuser?{% for %}'
|
||||||
|
caplog.clear()
|
||||||
|
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||||
|
context = cell.get_cell_extra_context({})
|
||||||
|
# can't get URL, variable is missing
|
||||||
|
assert context['json'] is None
|
||||||
|
assert requests_get.call_count == 0
|
||||||
|
assert len(caplog.records) == 1
|
||||||
|
assert caplog.record_tuples == [
|
||||||
|
(
|
||||||
|
'combo.data.models',
|
||||||
|
logging.ERROR,
|
||||||
|
'error in templated URL (http://testuser?{% for %}): syntax error',
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_json_cell_with_varnames(app):
|
def test_json_cell_with_varnames(app):
|
||||||
page = Page(title='example page', slug='index')
|
page = Page(title='example page', slug='index')
|
||||||
|
|
Loading…
Reference in New Issue