diff --git a/bi_view_editor/models/bve_view.py b/bi_view_editor/models/bve_view.py index 1c10716f..58ac76a8 100644 --- a/bi_view_editor/models/bve_view.py +++ b/bi_view_editor/models/bve_view.py @@ -295,8 +295,16 @@ class BveView(models.Model): self._cr.execute('DROP TABLE IF EXISTS %s', (AsIs(view_name), )) # create postgres view - self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', ( - AsIs(view_name), AsIs(query), )) + try: + with self.env.cr.savepoint(): + self.env.cr.execute('CREATE or REPLACE VIEW %s as (%s)', ( + AsIs(view_name), AsIs(query), )) + except Exception as e: + raise UserError( + _("Error creating the view '{query}':\n{error}") + .format( + query=query, + error=e)) @api.depends('line_ids', 'state', 'over_condition') def _compute_sql_query(self): diff --git a/bi_view_editor/readme/ROADMAP.rst b/bi_view_editor/readme/ROADMAP.rst index b7e7bd0f..32e4e2d0 100644 --- a/bi_view_editor/readme/ROADMAP.rst +++ b/bi_view_editor/readme/ROADMAP.rst @@ -5,4 +5,3 @@ * Data the user has no access to (e.g. in a multi company situation) can be viewed by making a view. Would be nice if models available to select when creating a view are limited to the ones that have intersecting groups. -* Raise a warning in case the SQL query is not correct. diff --git a/bi_view_editor/tests/test_bi_view.py b/bi_view_editor/tests/test_bi_view.py index 38e303b8..7404dcd9 100644 --- a/bi_view_editor/tests/test_bi_view.py +++ b/bi_view_editor/tests/test_bi_view.py @@ -5,6 +5,7 @@ import json import odoo from odoo.tests.common import TransactionCase +from odoo.tools import mute_logger from odoo.exceptions import UserError, ValidationError from ..hooks import post_load, uninstall_hook @@ -407,3 +408,24 @@ class TestBiViewEditor(TransactionCase): bi_view1 = self.env['bve.view'].create(vals) bi_view1.action_create() self.assertEqual(len(bi_view1.line_ids), 4) + + @mute_logger('odoo.sql_db') + def test_20_broken_view(self): + """ + Create a broken query, a nice UserError should be raised. + odoo.sql_db logger is muted to avoid the + ERROR: bad_query line in the logs. + """ + vals = self.bi_view1_vals + vals.update({ + 'name': 'Test View broken', + 'over_condition': 'bad SQL code', + }) + bi_view = self.env['bve.view'].create(vals) + with self.assertRaises(UserError) as ue: + bi_view.action_create() + + self.assertEqual(bi_view.state, 'draft') + self.assertIn(bi_view.over_condition, str(ue.exception)) + # remove view + bi_view.unlink()