Handle API error messages on add/edit user

Display API error messages on Local user managment tab.

Change-Id: I8ffe2359aa454503d07e8895907202bd0d8493f5
Signed-off-by: MichalX Szopinski <michalx.szopinski@intel.com>
diff --git a/src/store/modules/SecurityAndAccess/UserManagementStore.js b/src/store/modules/SecurityAndAccess/UserManagementStore.js
index 78dbd43..3bdf91d 100644
--- a/src/store/modules/SecurityAndAccess/UserManagementStore.js
+++ b/src/store/modules/SecurityAndAccess/UserManagementStore.js
@@ -1,6 +1,19 @@
 import api, { getResponseCount } from '@/store/api';
 import i18n from '@/i18n';
 
+const getServerErrorMessages = function (error) {
+  let errorData = error.response.data.error
+    ? error.response.data.error
+    : error.response.data;
+  if (typeof errorData == 'string') {
+    return [];
+  }
+  return Object.values(errorData)
+    .reduce((a, b) => a.concat(b))
+    .filter((info) => info.Message)
+    .map((info) => info.Message);
+};
+
 const UserManagementStore = {
   namespaced: true,
   state: {
@@ -113,27 +126,13 @@
           })
         )
         .catch((error) => {
-          let message = i18n.t('pageUserManagement.toast.errorCreateUser', {
-            username,
-          });
-          if (error.response && error.response.data) {
-            if (error.response.data['UserName@Message.ExtendedInfo']) {
-              let obj = error.response.data['UserName@Message.ExtendedInfo'];
-              for (var key in obj) {
-                if (obj[key].Message) {
-                  let msg = obj[key].Message;
-                  if (msg.indexOf('already exists') != -1) {
-                    message = i18n.t(
-                      'pageUserManagement.toast.errorAlreadyExistUser',
-                      {
-                        username,
-                      }
-                    );
-                  }
-                }
-              }
-            }
-          }
+          let serverMessages = getServerErrorMessages(error);
+          let message =
+            serverMessages.length > 0
+              ? serverMessages.join(' ')
+              : i18n.t('pageUserManagement.toast.errorCreateUser', {
+                  username: username,
+                });
           throw new Error(message);
         });
     },
@@ -157,9 +156,13 @@
         )
         .catch((error) => {
           console.log(error);
-          const message = i18n.t('pageUserManagement.toast.errorUpdateUser', {
-            username: originalUsername,
-          });
+          const serverMessages = getServerErrorMessages(error);
+          const message =
+            serverMessages.length > 0
+              ? serverMessages.join(' ')
+              : i18n.t('pageUserManagement.toast.errorUpdateUser', {
+                  username: originalUsername,
+                });
           throw new Error(message);
         });
     },