From 959f2ebde1e9920876f6f6c72ea0cbeccf4235ad Mon Sep 17 00:00:00 2001
From: Nicolas Giard <github@ngpixel.com>
Date: Sat, 12 Jan 2019 22:33:30 -0500
Subject: [PATCH] fix: graphql error notifications

---
 client/components/admin/admin-auth.vue        | 50 ++++++++++---------
 client/components/admin/admin-general.vue     |  6 +--
 client/components/admin/admin-groups-edit.vue | 12 +----
 client/components/admin/admin-groups.vue      |  6 +--
 client/components/admin/admin-mail.vue        |  6 +--
 client/components/admin/admin-navigation.vue  |  6 +--
 client/components/admin/admin-theme.vue       |  6 +--
 client/store/index.js                         |  7 +++
 server/graph/resolvers/group.js               |  1 +
 9 files changed, 42 insertions(+), 58 deletions(-)

diff --git a/client/components/admin/admin-auth.vue b/client/components/admin/admin-auth.vue
index 23b8982f..a66023e4 100644
--- a/client/components/admin/admin-auth.vue
+++ b/client/components/admin/admin-auth.vue
@@ -204,29 +204,33 @@ export default {
     },
     async save() {
       this.$store.commit(`loadingStart`, 'admin-auth-savestrategies')
-      await this.$apollo.mutate({
-        mutation: strategiesSaveMutation,
-        variables: {
-          config: {
-            audience: this.jwtAudience,
-            tokenExpiration: this.jwtExpiration,
-            tokenRenewal: this.jwtRenewablePeriod
-          },
-          strategies: this.strategies.map(str => _.pick(str, [
-            'isEnabled',
-            'key',
-            'config',
-            'selfRegistration',
-            'domainWhitelist',
-            'autoEnrollGroups'
-          ])).map(str => ({...str, config: str.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })}))}))
-        }
-      })
-      this.$store.commit('showNotification', {
-        message: 'Authentication configuration saved successfully.',
-        style: 'success',
-        icon: 'check'
-      })
+      try {
+        await this.$apollo.mutate({
+          mutation: strategiesSaveMutation,
+          variables: {
+            config: {
+              audience: this.jwtAudience,
+              tokenExpiration: this.jwtExpiration,
+              tokenRenewal: this.jwtRenewablePeriod
+            },
+            strategies: this.strategies.map(str => _.pick(str, [
+              'isEnabled',
+              'key',
+              'config',
+              'selfRegistration',
+              'domainWhitelist',
+              'autoEnrollGroups'
+            ])).map(str => ({...str, config: str.config.map(cfg => ({...cfg, value: JSON.stringify({ v: cfg.value.value })}))}))
+          }
+        })
+        this.$store.commit('showNotification', {
+          message: 'Authentication configuration saved successfully.',
+          style: 'success',
+          icon: 'check'
+        })
+      } catch (err) {
+        this.$store.commit('pushGraphError', err)
+      }
       this.$store.commit(`loadingStop`, 'admin-auth-savestrategies')
     }
   },
diff --git a/client/components/admin/admin-general.vue b/client/components/admin/admin-general.vue
index 6efe8787..81eadc3c 100644
--- a/client/components/admin/admin-general.vue
+++ b/client/components/admin/admin-general.vue
@@ -229,11 +229,7 @@ export default {
         this.siteTitle = this.config.title
         this.company = this.config.company
       } catch (err) {
-        this.$store.commit('showNotification', {
-          style: 'red',
-          message: err.message,
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
     }
   },
diff --git a/client/components/admin/admin-groups-edit.vue b/client/components/admin/admin-groups-edit.vue
index 215c9042..65fc4f6b 100644
--- a/client/components/admin/admin-groups-edit.vue
+++ b/client/components/admin/admin-groups-edit.vue
@@ -93,11 +93,7 @@ export default {
           icon: 'check'
         })
       } catch (err) {
-        this.$store.commit('showNotification', {
-          style: 'red',
-          message: err.message,
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
     },
     async deleteGroup() {
@@ -119,11 +115,7 @@ export default {
         })
         this.$router.replace('/groups')
       } catch (err) {
-        this.$store.commit('showNotification', {
-          style: 'red',
-          message: err.message,
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
     },
     async refresh() {
diff --git a/client/components/admin/admin-groups.vue b/client/components/admin/admin-groups.vue
index 0a43b695..82b0a731 100644
--- a/client/components/admin/admin-groups.vue
+++ b/client/components/admin/admin-groups.vue
@@ -149,11 +149,7 @@ export default {
           icon: 'check'
         })
       } catch (err) {
-        this.$store.commit('showNotification', {
-          style: 'red',
-          message: err.message,
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
     }
   },
diff --git a/client/components/admin/admin-mail.vue b/client/components/admin/admin-mail.vue
index ba0eab4b..292331e1 100644
--- a/client/components/admin/admin-mail.vue
+++ b/client/components/admin/admin-mail.vue
@@ -180,11 +180,7 @@ export default {
           icon: 'check'
         })
       } catch (err) {
-        this.$store.commit('showNotification', {
-          style: 'red',
-          message: err.message,
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
     }
   },
diff --git a/client/components/admin/admin-navigation.vue b/client/components/admin/admin-navigation.vue
index 3f106ab8..3488eedd 100644
--- a/client/components/admin/admin-navigation.vue
+++ b/client/components/admin/admin-navigation.vue
@@ -214,11 +214,7 @@ export default {
           throw new Error(_.get(resp, 'data.navigation.updateTree.responseResult.message', 'An unexpected error occured.'))
         }
       } catch (err) {
-        this.$store.commit('showNotification', {
-          message: err.message,
-          style: 'red',
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
       this.$store.commit(`loadingStop`, 'admin-navigation-save')
     },
diff --git a/client/components/admin/admin-theme.vue b/client/components/admin/admin-theme.vue
index 20aaa80d..0909e446 100644
--- a/client/components/admin/admin-theme.vue
+++ b/client/components/admin/admin-theme.vue
@@ -146,11 +146,7 @@ export default {
           throw new Error(resp.message)
         }
       } catch (err) {
-        this.$store.commit('showNotification', {
-          message: `Error: ${err.message}`,
-          style: 'error',
-          icon: 'warning'
-        })
+        this.$store.commit('pushGraphError', err)
       }
       this.$store.commit(`loadingStop`, 'admin-theme-save')
       this.loading = false
diff --git a/client/store/index.js b/client/store/index.js
index c93fd670..efd970f3 100644
--- a/client/store/index.js
+++ b/client/store/index.js
@@ -47,6 +47,13 @@ export default new Vuex.Store({
     },
     updateNotificationState (state, newState) {
       state.notification.isActive = newState
+    },
+    pushGraphError (state, err) {
+      WIKI.$store.commit('showNotification', {
+        style: 'red',
+        message: _.get(err, 'graphQLErrors[0].message', err.message),
+        icon: 'warning'
+      })
     }
   },
   actions: { },
diff --git a/server/graph/resolvers/group.js b/server/graph/resolvers/group.js
index 69a15f31..00f109d7 100644
--- a/server/graph/resolvers/group.js
+++ b/server/graph/resolvers/group.js
@@ -1,5 +1,6 @@
 const graphHelper = require('../../helpers/graph')
 const safeRegex = require('safe-regex')
+const _ = require('lodash')
 
 /* global WIKI */
 
-- 
2.24.1