From 29be87f96dd525044558d7fd731537356b9d8b25 Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Sun, 8 Jan 2023 15:37:31 +0100
Subject: [PATCH] Use more general and translated error messages for GraphQL
 and Network error snackbars

---
 aleksis/core/assets/app.js                      | 13 +++++--------
 aleksis/core/assets/components/SnackbarItem.vue |  2 +-
 aleksis/core/assets/messages.json               |  6 +++++-
 aleksis/core/assets/snackbarItems.graphql       |  2 +-
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/aleksis/core/assets/app.js b/aleksis/core/assets/app.js
index 748747e09..54dc63020 100644
--- a/aleksis/core/assets/app.js
+++ b/aleksis/core/assets/app.js
@@ -67,7 +67,7 @@ Vue.use(VueRouter);
 export const typeDefs = gql`
   type snackbarItem {
     id: ID!
-    message: String!
+    messageKey: String!
     color: String!
     read: Boolean!
   }
@@ -100,7 +100,7 @@ const resolvers = {
   },
 };
 
-function addErrorSnackbarItem (error) {
+function addErrorSnackbarItem (messageKey) {
   let uuid = crypto.randomUUID();
   cache.writeQuery({
     query: gqlSnackbarItems,
@@ -109,7 +109,7 @@ function addErrorSnackbarItem (error) {
         {
           __typename: "snackbarItem",
           id: uuid,
-          message: error,
+          messageKey: messageKey,
           color: "red",
           read: false
         },
@@ -124,12 +124,9 @@ function addErrorSnackbarItem (error) {
 const retryLink = new RetryLink();
 
 const errorLink = onError(({ graphQLErrors, networkError }) => {
-  if (graphQLErrors)
-    graphQLErrors.map(({ message, locations, path }) =>
-      addErrorSnackbarItem(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`),
-    )
+  if (graphQLErrors) addErrorSnackbarItem("graphql_errors.snackbar_error_message")
 
-  if (networkError) addErrorSnackbarItem(`[Network error]: ${networkError}`)
+  if (networkError) addErrorSnackbarItem("network_errors.snackbar_error_message")
 });
 
 const httpLink = new HttpLink({
diff --git a/aleksis/core/assets/components/SnackbarItem.vue b/aleksis/core/assets/components/SnackbarItem.vue
index b7b496d73..f80fb4cc7 100644
--- a/aleksis/core/assets/components/SnackbarItem.vue
+++ b/aleksis/core/assets/components/SnackbarItem.vue
@@ -1,6 +1,6 @@
 <template>
   <v-snackbar v-model="!snackbarItem.read" :color="snackbarItem.color">
-    {{ snackbarItem.message }}
+    {{ $t(snackbarItem.messageKey) }}
     <template v-slot:action="{ attrs }">
       <v-btn icon @click="checkSnackbarItem(snackbarItem.id)" ><v-icon>mdi-close</v-icon></v-btn>
     </template>
diff --git a/aleksis/core/assets/messages.json b/aleksis/core/assets/messages.json
index 5bfa70fb7..64cc32934 100644
--- a/aleksis/core/assets/messages.json
+++ b/aleksis/core/assets/messages.json
@@ -239,7 +239,11 @@
     "network_errors": {
       "error_404": "404",
       "page_not_found": "The requested page or resource could not be found.",
-      "take_me_back": "Take me back"
+      "take_me_back": "Take me back",
+      "snackbar_error_message": "There was an error with your network. Please try again."
+    },
+    "graphql_errors": {
+      "snackbar_error_message": "There was an error accessing the page data. Please try again."
     },
     "service_worker": {
       "new_version_available": "A new version of the app is available",
diff --git a/aleksis/core/assets/snackbarItems.graphql b/aleksis/core/assets/snackbarItems.graphql
index 28f5034a3..c31423e87 100644
--- a/aleksis/core/assets/snackbarItems.graphql
+++ b/aleksis/core/assets/snackbarItems.graphql
@@ -1,7 +1,7 @@
 {
   snackbarItems @client {
     id
-    message
+    messageKey
     color
     read
   }
-- 
GitLab