about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/actions/alerts.js
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2019-02-10 21:28:29 +0100
committerThibaut Girka <thib@sitedethib.com>2019-02-10 21:44:14 +0100
commita963ea67dda17f69ed783b3fbcc91e5ce3858ad3 (patch)
tree86989e490ae6b7ed7460a4cb2345a48d56e5efcd /app/javascript/flavours/glitch/actions/alerts.js
parent6b2eefc7bf8cf1bb58da165d9594d9b17bf97e11 (diff)
[Glitch] Add missing rejection handling for Promises
Port missing parts from 2c51bc0ca5a4c3a4bb140b4b40dabdda859ebb94 to glitch-soc
Diffstat (limited to 'app/javascript/flavours/glitch/actions/alerts.js')
-rw-r--r--app/javascript/flavours/glitch/actions/alerts.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/javascript/flavours/glitch/actions/alerts.js b/app/javascript/flavours/glitch/actions/alerts.js
index f37fdeeb6..3f5d7ef46 100644
--- a/app/javascript/flavours/glitch/actions/alerts.js
+++ b/app/javascript/flavours/glitch/actions/alerts.js
@@ -1,3 +1,10 @@
+import { defineMessages } from 'react-intl';
+
+const messages = defineMessages({
+  unexpectedTitle: { id: 'alert.unexpected.title', defaultMessage: 'Oops!' },
+  unexpectedMessage: { id: 'alert.unexpected.message', defaultMessage: 'An unexpected error occurred.' },
+});
+
 export const ALERT_SHOW    = 'ALERT_SHOW';
 export const ALERT_DISMISS = 'ALERT_DISMISS';
 export const ALERT_CLEAR   = 'ALERT_CLEAR';
@@ -22,3 +29,21 @@ export function showAlert(title, message) {
     message,
   };
 };
+
+export function showAlertForError(error) {
+  if (error.response) {
+    const { data, status, statusText } = error.response;
+
+    let message = statusText;
+    let title   = `${status}`;
+
+    if (data.error) {
+      message = data.error;
+    }
+
+    return showAlert(title, message);
+  } else {
+    console.error(error);
+    return showAlert(messages.unexpectedTitle, messages.unexpectedMessage);
+  }
+}