From 32de851fa24552b4799fb67198b51dc602dd4690 Mon Sep 17 00:00:00 2001
From: Tomas Peterka <tomas.peterka@nexedi.com>
Date: Thu, 1 Feb 2018 11:37:35 +0100
Subject: [PATCH] [renderjs_ui] Dialog shows custom portal_message if included
 in HTTP500

/reviewed-on https://lab.nexedi.com/nexedi/erp5/merge_requests/570
---
 .../rjs_gadget_erp5_pt_form_dialog_js.js      | 22 ++++++++++++++-----
 .../rjs_gadget_erp5_pt_form_dialog_js.xml     |  4 ++--
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.js b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.js
index d3ed0f6883..0c9565b353 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.js
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.js
@@ -181,17 +181,29 @@
       .push(undefined, function (error) {
         if (error.target !== undefined) {
           var error_text = 'Encountered an unknown error. Try to resubmit',
-            promise;
-          // if we know what the error was, try to precise it for the user 
+            promise_queue = new RSVP.Queue();
+          // if we know what the error was, try to precise it for the user
           if (error.target.status === 400) {
             error_text = 'Input data has errors';
           } else if (error.target.status === 403) {
             error_text = 'You do not have the permissions to edit the object';
           } else if (error.target.status === 0) {
             error_text = 'Document was not saved! Resubmit when you are online or the document accessible';
+          } else if (error.target.status === 500 && error.target.response.type === "application/json") {
+            promise_queue
+              .push(function () {
+                return jIO.util.readBlobAsText(error.target.response);
+              })
+              .push(function (response_text) {
+                var response = JSON.parse(response_text.target.result);
+                error_text = response.portal_status_message;
+              });
           }
           // display translated error_text to user
-          promise = form_gadget.notifySubmitted()
+          promise_queue
+            .push(function () {
+              return form_gadget.notifySubmitted();
+            })
             .push(function () {
               return form_gadget.translate(error_text);
             })
@@ -204,7 +216,7 @@
           // if server validation of form data failed (indicated by response code 400)
           // we parse out field errors and display them to the user
           if (error.target.status === 400) {
-            promise
+            promise_queue
               .push(function () {
                 // when the server-side validation returns the error description
                 if (error.target.responseType === "blob") {
@@ -217,7 +229,7 @@
                 return form_gadget.displayFormulatorValidationError(JSON.parse(event.target.result));
               });
           }
-          return promise;
+          return promise_queue;
         }
         throw error;
       });
diff --git a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.xml b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.xml
index 97fc9759e6..e3474998e9 100644
--- a/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.xml
+++ b/bt5/erp5_web_renderjs_ui/PathTemplateItem/web_page_module/rjs_gadget_erp5_pt_form_dialog_js.xml
@@ -230,7 +230,7 @@
             </item>
             <item>
                 <key> <string>serial</string> </key>
-                <value> <string>965.10338.33096.47104</string> </value>
+                <value> <string>965.13214.39160.9318</string> </value>
             </item>
             <item>
                 <key> <string>state</string> </key>
@@ -248,7 +248,7 @@
                     </tuple>
                     <state>
                       <tuple>
-                        <float>1517305313.45</float>
+                        <float>1517480919.98</float>
                         <string>UTC</string>
                       </tuple>
                     </state>
-- 
2.30.9