From f115ecfdf9d68a416c38d2cb7c0f73e93ea2ea5e Mon Sep 17 00:00:00 2001
From: stcc-odoo <stcc@odoo.com>
Date: Mon, 17 Jul 2023 14:30:38 +0200
Subject: [PATCH] [FIX] web: show warning instead of failing silently

Steps to reproduce:

- Install Accounting
- Accounting > Accounting > Journal Items
- Select all items using the checkbox
- Change account to another value
- Click "OK" on the confirmation dialog

Issue:

The system fails to change the account field on the records. This issue occurs because the JavaScript code catches an RPCError when attempting
to write the values to the database but ignores it. Although this
behavior is intended, it leaves the user without any feedback
throughout the process.

Solution:

Add a danger notification with the error message when the write
operation fails.

opw-3354646

closes odoo/odoo#128736

Signed-off-by: Francois Georis (fge) <fge@odoo.com>
---
 addons/web/static/src/views/relational_model.js  |  9 ++++++++-
 addons/web/static/tests/views/list_view_tests.js | 12 ++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/addons/web/static/src/views/relational_model.js b/addons/web/static/src/views/relational_model.js
index 69d273ff81fd..56d99e66cde1 100644
--- a/addons/web/static/src/views/relational_model.js
+++ b/addons/web/static/src/views/relational_model.js
@@ -10,6 +10,7 @@ import {
     serializeDate,
     serializeDateTime,
 } from "@web/core/l10n/dates";
+import { RPCError } from "@web/core/network/rpc_service";
 import { ORM, x2ManyCommands } from "@web/core/orm_service";
 import { evaluateExpr } from "@web/core/py_js/py";
 import { registry } from "@web/core/registry";
@@ -1664,8 +1665,14 @@ class DynamicList extends DataPoint {
                         await Promise.all(validSelection.map((record) => record.load()));
                         record.switchMode("readonly");
                         this.model.notify();
-                    } catch (_) {
+                    } catch (e) {
                         record.discard();
+                        const errorMessage = e instanceof RPCError ? e.data.message : e.message;
+                        const errorTitle = e instanceof RPCError ? e.message : this.model.env._t("Error");
+                        this.model.notificationService.add(errorMessage, {
+                            title: errorTitle,
+                            type: "danger",
+                        });
                     }
                     validSelection.forEach((record) => {
                         record.selected = false;
diff --git a/addons/web/static/tests/views/list_view_tests.js b/addons/web/static/tests/views/list_view_tests.js
index 11d5b5c99c0f..8e30954cff9a 100644
--- a/addons/web/static/tests/views/list_view_tests.js
+++ b/addons/web/static/tests/views/list_view_tests.js
@@ -11292,17 +11292,23 @@ QUnit.module("Views", (hooks) => {
     });
 
     QUnit.test("editable list view: multi edition server error handling", async function (assert) {
-        await makeView({
+        const list = await makeView({
             type: "list",
             resModel: "foo",
             serverData,
             arch: '<tree multi_edit="1"><field name="foo" required="1"/></tree>',
             mockRPC(route, args) {
                 if (args.method === "write") {
-                    return Promise.reject();
+                    return Promise.reject({ message: "Odoo Server Error" });
                 }
             },
         });
+        patchWithCleanup(list.env.services.notification, {
+            add: (message) => {
+                assert.equal(message, "Odoo Server Error");
+                assert.step("Error");
+            },
+        });
 
         // select two records
         const rows = target.querySelectorAll(".o_data_row");
@@ -11313,7 +11319,9 @@ QUnit.module("Views", (hooks) => {
         await click(rows[0].querySelector(".o_data_cell"));
         await editInput(target, ".o_selected_row [name=foo] input", "abc");
         await click(target, ".o_list_view");
+        assert.verifySteps([]);
         await click(target, ".modal .btn-primary");
+        assert.verifySteps(["Error"]);
         // Server error: if there was a crash manager, there would be an open error at this point...
         assert.strictEqual(
             $(target).find(".o_data_row:eq(0) .o_data_cell").text(),
-- 
GitLab