cells: jsoncell log error on syntax error only (#57626)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-10-07 11:44:51 +02:00
parent e1dcad5831
commit e415373110
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 44 additions and 2 deletions

View File

@ -1852,7 +1852,7 @@ class JsonCellBase(CellBase):
url = utils.get_templated_url(data_url_dict['url'], context)
except utils.TemplateError as e:
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)
else:
logger.debug('error in templated URL (%s): %s', data_url_dict['url'], e)

View File

@ -1,5 +1,6 @@
import datetime
import json
import logging
import os
import re
from unittest import mock
@ -459,7 +460,8 @@ def mocked_request(*args, **kwargs):
pass
def test_json_cell():
def test_json_cell(caplog):
caplog.set_level(logging.DEBUG)
page = Page(title='example page', slug='example-page')
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[1]['cache_duration'] == 10
assert requests_get.call_count == 1
caplog.clear()
with mock.patch('combo.utils.requests.get') as requests_get:
context = cell.get_cell_extra_context({})
# can't get URL, 'foobar' 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?[foobar]): unknown variable foobar',
)
]
request = RequestFactory().get('/')
request.user = User(username='foo', email='foo@example.net')
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[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):
page = Page(title='example page', slug='index')