From 2d553407976c539af4af8764e835313a059cdf4c Mon Sep 17 00:00:00 2001 From: Karnav Sojitra Date: Fri, 11 Aug 2023 07:25:14 +0000 Subject: [PATCH] [FIX] tools: raise validation error while invalid expression When the user tries to modify the view with an invalid xpath expression, an XPathSyntaxError traceback will appear. Steps to produce: 1. Install the Accounting module. 2. Settings > Technical > UI > Views > Open any view 3. Invalidate expr syntax and try to save, thus an error will be generated. Error: XPathSyntaxError: Invalid expression This commit handles XPathSyntaxError by raising ValidationError instead of a traceback. sentry-4377014622 closes odoo/odoo#139435 X-original-commit: 2af583d0b803b3334b871002e447d3211eb09bc2 Signed-off-by: Achraf Ben Azzouz (abz) --- odoo/addons/base/i18n/base.pot | 6 ++++++ odoo/tools/template_inheritance.py | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/odoo/addons/base/i18n/base.pot b/odoo/addons/base/i18n/base.pot index 7c9137a7e96..b56b41bee7f 100644 --- a/odoo/addons/base/i18n/base.pot +++ b/odoo/addons/base/i18n/base.pot @@ -18238,6 +18238,12 @@ msgstr "" msgid "Invalid 'group by' parameter" msgstr "" +#. module: base +#: code:addons/template_inheritance.py:0 +#, python-format +msgid "Invalid Expression while parsing xpath %r" +msgstr "" + #. module: base #: code:addons/base/models/ir_default.py:0 #, python-format diff --git a/odoo/tools/template_inheritance.py b/odoo/tools/template_inheritance.py index 7d7b7a49a82..ac1aba0f2f8 100644 --- a/odoo/tools/template_inheritance.py +++ b/odoo/tools/template_inheritance.py @@ -8,6 +8,7 @@ import re from odoo.tools.translate import _ from odoo.tools import SKIPPED_ELEMENT_TYPES, html_escape +from odoo.exceptions import ValidationError _logger = logging.getLogger(__name__) RSTRIP_REGEXP = re.compile(r'\n[ \t]*$') @@ -74,9 +75,8 @@ def locate_node(arch, spec): expr = spec.get('expr') try: xPath = etree.ETXPath(expr) - except etree.XPathSyntaxError: - _logger.error("XPathSyntaxError while parsing xpath %r", expr) - raise + except etree.XPathSyntaxError as e: + raise ValidationError(_("Invalid Expression while parsing xpath %r", expr)) from e nodes = xPath(arch) return nodes[0] if nodes else None elif spec.tag == 'field':