]> cat aescling's git repositories - mastodon.git/commitdiff
[Glitch] Add missing rejection handling for Promises
authorThibaut Girka <thib@sitedethib.com>
Sun, 10 Feb 2019 20:28:29 +0000 (21:28 +0100)
committerThibaut Girka <thib@sitedethib.com>
Sun, 10 Feb 2019 20:44:14 +0000 (21:44 +0100)
Port missing parts from 2c51bc0ca5a4c3a4bb140b4b40dabdda859ebb94 to glitch-soc

app/javascript/flavours/glitch/actions/alerts.js
app/javascript/flavours/glitch/actions/compose.js
app/javascript/flavours/glitch/actions/lists.js
app/javascript/flavours/glitch/actions/push_notifications/registerer.js
app/javascript/flavours/glitch/actions/settings.js
app/javascript/flavours/glitch/containers/status_container.js
app/javascript/flavours/glitch/features/ui/components/embed_modal.js
app/javascript/flavours/glitch/middleware/errors.js

index f37fdeeb6ab353724544e86231972d55f72b6185..3f5d7ef46b198949838f880b14f84d94365bdf45 100644 (file)
@@ -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);
+  }
+}
index 04e8577132161785a5bb6971edb26ab53478ce98..204d609b0d795cf7d75adc41990393e51870fc4b 100644 (file)
@@ -1,5 +1,5 @@
 import api from 'flavours/glitch/util/api';
-import { CancelToken } from 'axios';
+import { CancelToken, isCancel } from 'axios';
 import { throttle } from 'lodash';
 import { search as emojiSearch } from 'flavours/glitch/util/emoji/emoji_mart_search_light';
 import { useEmoji } from './emojis';
@@ -8,6 +8,7 @@ import { recoverHashtags } from 'flavours/glitch/util/hashtag';
 import resizeImage from 'flavours/glitch/util/resize_image';
 
 import { updateTimeline } from './timelines';
+import { showAlertForError } from './alerts';
 
 let cancelFetchComposeSuggestionsAccounts;
 
@@ -320,6 +321,10 @@ const fetchComposeSuggestionsAccounts = throttle((dispatch, getState, token) =>
     },
   }).then(response => {
     dispatch(readyComposeSuggestionsAccounts(token, response.data));
+  }).catch(error => {
+    if (!isCancel(error)) {
+      dispatch(showAlertForError(error));
+    }
   });
 }, 200, { leading: true, trailing: true });
 
index 7d94ee950ef1b4a05d1c0ac5fefad955d7e55d59..f29ca1e0103d0cf364257fb92213125a42f330e0 100644 (file)
@@ -1,4 +1,5 @@
 import api from 'flavours/glitch/util/api';
+import { showAlertForError } from './alerts';
 
 export const LIST_FETCH_REQUEST = 'LIST_FETCH_REQUEST';
 export const LIST_FETCH_SUCCESS = 'LIST_FETCH_SUCCESS';
@@ -239,7 +240,8 @@ export const fetchListSuggestions = q => (dispatch, getState) => {
   };
 
   api(getState).get('/api/v1/accounts/search', { params })
-    .then(({ data }) => dispatch(fetchListSuggestionsReady(q, data)));
+    .then(({ data }) => dispatch(fetchListSuggestionsReady(q, data)))
+    .catch(error => dispatch(showAlertForError(error)));
 };
 
 export const fetchListSuggestionsReady = (query, accounts) => ({
index 91f442415adae40fae5ff7fc874d31add46ea289..8fdb239f7218e00b353388db2996d7921d55846a 100644 (file)
@@ -109,14 +109,11 @@ export function register () {
             pushNotificationsSetting.remove(me);
           }
 
-          try {
-            getRegistration()
-              .then(getPushSubscription)
-              .then(unsubscribe);
-          } catch (e) {
-
-          }
-        });
+          return getRegistration()
+            .then(getPushSubscription)
+            .then(unsubscribe);
+        })
+        .catch(console.warn);
     } else {
       console.warn('Your browser does not support Web Push Notifications.');
     }
@@ -137,6 +134,6 @@ export function saveSettings() {
       if (me) {
         pushNotificationsSetting.set(me, data);
       }
-    });
+    }).catch(console.warn);
   };
 }
index 87b2ae76d9f3198fd08602e1669151b3912568c1..fb0bcc09ce32d468de124c20abb9f930de1f2af0 100644 (file)
@@ -1,5 +1,6 @@
 import api from 'flavours/glitch/util/api';
 import { debounce } from 'lodash';
+import { showAlertForError } from './alerts';
 
 export const SETTING_CHANGE = 'SETTING_CHANGE';
 export const SETTING_SAVE   = 'SETTING_SAVE';
@@ -23,7 +24,9 @@ const debouncedSave = debounce((dispatch, getState) => {
 
   const data = getState().get('settings').filter((_, path) => path !== 'saved').toJS();
 
-  api(getState).put('/api/web/settings', { data }).then(() => dispatch({ type: SETTING_SAVE }));
+  api(getState).put('/api/web/settings', { data })
+    .then(() => dispatch({ type: SETTING_SAVE }))
+    .catch(error => dispatch(showAlertForError(error)));
 }, 5000, { trailing: true });
 
 export function saveSettings() {
index f28dce609e2b677286df77bf5cee6c394300837d..f783878b00112a87fb72c59580558a19a348a598 100644 (file)
@@ -25,6 +25,7 @@ import { openModal } from 'flavours/glitch/actions/modal';
 import { changeLocalSetting } from 'flavours/glitch/actions/local_settings';
 import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
 import { boostModal, favouriteModal, deleteModal } from 'flavours/glitch/util/initial_state';
+import { showAlertForError } from '../actions/alerts';
 
 const messages = defineMessages({
   deleteConfirm: { id: 'confirmations.delete.confirm', defaultMessage: 'Delete' },
@@ -134,7 +135,10 @@ const mapDispatchToProps = (dispatch, { intl }) => ({
   },
 
   onEmbed (status) {
-    dispatch(openModal('EMBED', { url: status.get('url') }));
+    dispatch(openModal('EMBED', {
+      url: status.get('url'),
+      onError: error => dispatch(showAlertForError(error)),
+    }));
   },
 
   onDelete (status, history, withRedraft = false) {
index bf29b0da5d87246da8a5dd3b70cba01c436dbf23..b1643df1cb7b78185629a4b3de0e31ba03949f86 100644 (file)
@@ -10,6 +10,7 @@ export default class EmbedModal extends ImmutablePureComponent {
   static propTypes = {
     url: PropTypes.string.isRequired,
     onClose: PropTypes.func.isRequired,
+    onError: PropTypes.func.isRequired,
     intl: PropTypes.object.isRequired,
   }
 
@@ -35,6 +36,8 @@ export default class EmbedModal extends ImmutablePureComponent {
       iframeDocument.body.style.margin = 0;
       this.iframe.width  = iframeDocument.body.scrollWidth;
       this.iframe.height = iframeDocument.body.scrollHeight;
+    }).catch(error => {
+      this.props.onError(error);
     });
   }
 
index f3dfc8b069e82cd038c9d205b852e02a1a7702a1..212c1f4ad2c9ea54d564997508ff52d2f50bdd0b 100644 (file)
@@ -1,4 +1,4 @@
-import { showAlert } from 'flavours/glitch/actions/alerts';
+import { showAlertForError } from 'flavours/glitch/actions/alerts';
 
 const defaultFailSuffix = 'FAIL';
 
@@ -8,21 +8,7 @@ export default function errorsMiddleware() {
       const isFail = new RegExp(`${defaultFailSuffix}$`, 'g');
 
       if (action.type.match(isFail)) {
-        if (action.error.response) {
-          const { data, status, statusText } = action.error.response;
-
-          let message = statusText;
-          let title   = `${status}`;
-
-          if (data.error) {
-            message = data.error;
-          }
-
-          dispatch(showAlert(title, message));
-        } else {
-          console.error(action.error);
-          dispatch(showAlert('Oops!', 'An unexpected error occurred.'));
-        }
+        dispatch(showAlertForError(action.error));
       }
     }