From 15b3fa83128df6603a44e755e0f9e95ef7cc8197 Mon Sep 17 00:00:00 2001 From: shakti97 Date: Sat, 25 Apr 2020 20:18:39 +0530 Subject: [PATCH] Fix(axios error handling issue) --- client/modules/IDE/actions/collections.js | 18 ++++--- client/modules/IDE/actions/files.js | 25 +++++---- client/modules/IDE/actions/preferences.js | 11 ++-- client/modules/IDE/actions/project.js | 35 ++++++++----- client/modules/IDE/actions/projects.js | 3 +- client/modules/IDE/actions/uploader.js | 3 +- client/modules/User/actions.js | 64 ++++++++++++++++------- 7 files changed, 106 insertions(+), 53 deletions(-) diff --git a/client/modules/IDE/actions/collections.js b/client/modules/IDE/actions/collections.js index 3bd99dc1..03cf2a64 100644 --- a/client/modules/IDE/actions/collections.js +++ b/client/modules/IDE/actions/collections.js @@ -27,7 +27,8 @@ export function getCollections(username) { }); dispatch(stopLoader()); }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data @@ -57,7 +58,8 @@ export function createCollection(collection) { browserHistory.push(location); }) - .catch((response) => { + .catch((error) => { + const { response } = error; console.error('Error creating collection', response.data); dispatch({ type: ActionTypes.ERROR, @@ -87,7 +89,8 @@ export function addToCollection(collectionId, projectId) { return response.data; }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data @@ -118,7 +121,8 @@ export function removeFromCollection(collectionId, projectId) { return response.data; }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data @@ -141,7 +145,8 @@ export function editCollection(collectionId, { name, description }) { }); return response.data; }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data @@ -164,7 +169,8 @@ export function deleteCollection(collectionId) { }); return response.data; }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 5edcf74d..004600b2 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -65,10 +65,13 @@ export function createFile(formProps) { // }); dispatch(setUnsavedChanges(true)); }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - error: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + error: response.data + }); + }); } else { const id = objectID().toHexString(); dispatch({ @@ -113,10 +116,13 @@ export function createFolder(formProps) { dispatch(setProjectSavedTime(response.data.project.updatedAt)); dispatch(closeNewFolderModal()); }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - error: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + error: response.data + }); + }); } else { const id = objectID().toHexString(); dispatch({ @@ -163,7 +169,8 @@ export function deleteFile(id, parentId) { parentId }); }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data diff --git a/client/modules/IDE/actions/preferences.js b/client/modules/IDE/actions/preferences.js index ae8e2438..01ba077c 100644 --- a/client/modules/IDE/actions/preferences.js +++ b/client/modules/IDE/actions/preferences.js @@ -8,10 +8,13 @@ function updatePreferences(formParams, dispatch) { axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true }) .then(() => { }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - error: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + error: response.data + }); + }); } export function setFontSize(value) { diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 2621d5c6..192e55dd 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -57,10 +57,13 @@ export function getProject(id) { dispatch(setProject(response.data)); dispatch(setUnsavedChanges(false)); }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - error: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + error: response.data + }); + }); }; } @@ -161,7 +164,8 @@ export function saveProject(selectedFile = null, autosave = false) { } } }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch(endSavingProject()); if (response.status === 403) { dispatch(showErrorModal('staleSession')); @@ -200,7 +204,8 @@ export function saveProject(selectedFile = null, autosave = false) { } } }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch(endSavingProject()); if (response.status === 403) { dispatch(showErrorModal('staleSession')); @@ -298,10 +303,13 @@ export function cloneProject(id) { browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`); dispatch(setNewProject(response.data)); }) - .catch(response => dispatch({ - type: ActionTypes.PROJECT_SAVE_FAIL, - error: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.PROJECT_SAVE_FAIL, + error: response.data + }); + }); }); }); }; @@ -344,8 +352,8 @@ export function changeProjectName(id, newName) { } } }) - .catch((response) => { - console.log(response); + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.PROJECT_SAVE_FAIL, error: response.data @@ -368,7 +376,8 @@ export function deleteProject(id) { id }); }) - .catch((response) => { + .catch((error) => { + const { response } = error; if (response.status === 403) { dispatch(showErrorModal('staleSession')); } else { diff --git a/client/modules/IDE/actions/projects.js b/client/modules/IDE/actions/projects.js index 446c50cc..eb653ec8 100644 --- a/client/modules/IDE/actions/projects.js +++ b/client/modules/IDE/actions/projects.js @@ -23,7 +23,8 @@ export function getProjects(username) { }); dispatch(stopLoader()); }) - .catch((response) => { + .catch((error) => { + const { response } = error; dispatch({ type: ActionTypes.ERROR, error: response.data diff --git a/client/modules/IDE/actions/uploader.js b/client/modules/IDE/actions/uploader.js index 602ac3e5..c7a0139f 100644 --- a/client/modules/IDE/actions/uploader.js +++ b/client/modules/IDE/actions/uploader.js @@ -65,7 +65,8 @@ export function dropzoneAcceptCallback(userId, file, done) { file.previewTemplate.className += ' uploading'; // eslint-disable-line done(); }) - .catch((response) => { + .catch((error) => { + const { response } = error; file.custom_status = 'rejected'; // eslint-disable-line if (response.data && response.data.responseText && response.data.responseText.message) { done(response.data.responseText.message); diff --git a/client/modules/User/actions.js b/client/modules/User/actions.js index 3793b2dc..19928549 100644 --- a/client/modules/User/actions.js +++ b/client/modules/User/actions.js @@ -26,7 +26,10 @@ export function signUpUser(previousPath, formValues) { dispatch(justOpenedProject()); browserHistory.push(previousPath); }) - .catch(response => dispatch(authError(response.data.error))); + .catch((error) => { + const { response } = error; + dispatch(authError(response.data.error)); + }); }; } @@ -82,7 +85,8 @@ export function getUser() { preferences: response.data.preferences }); }) - .catch((response) => { + .catch((error) => { + const { response } = error; const message = response.message || response.data.error; dispatch(authError(message)); }); @@ -98,7 +102,8 @@ export function validateSession() { dispatch(showErrorModal('staleSession')); } }) - .catch((response) => { + .catch((error) => { + const { response } = error; if (response.status === 404) { dispatch(showErrorModal('staleSession')); } @@ -114,7 +119,10 @@ export function logoutUser() { type: ActionTypes.UNAUTH_USER }); }) - .catch(response => dispatch(authError(response.data.error))); + .catch((error) => { + const { response } = error; + dispatch(authError(response.data.error)); + }); }; } @@ -127,10 +135,13 @@ export function initiateResetPassword(formValues) { .then(() => { // do nothing }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - message: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + message: response.data + }); + }); }; } @@ -143,10 +154,13 @@ export function initiateVerification() { .then(() => { // do nothing }) - .catch(response => dispatch({ - type: ActionTypes.ERROR, - message: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.ERROR, + message: response.data + }); + }); }; } @@ -161,10 +175,13 @@ export function verifyEmailConfirmation(token) { type: ActionTypes.EMAIL_VERIFICATION_VERIFIED, message: response.data, })) - .catch(response => dispatch({ - type: ActionTypes.EMAIL_VERIFICATION_INVALID, - message: response.data - })); + .catch((error) => { + const { response } = error; + dispatch({ + type: ActionTypes.EMAIL_VERIFICATION_INVALID, + message: response.data + }); + }); }; } @@ -216,7 +233,10 @@ export function updateSettings(formValues) { dispatch(showToast(5500)); dispatch(setToastText('Settings saved.')); }) - .catch(response => Promise.reject(new Error(response.data.error))); + .catch((error) => { + const { response } = error; + Promise.reject(new Error(response.data.error)); + }); } export function createApiKeySuccess(user) { @@ -232,7 +252,10 @@ export function createApiKey(label) { .then((response) => { dispatch(createApiKeySuccess(response.data)); }) - .catch(response => Promise.reject(new Error(response.data.error))); + .catch((error) => { + const { response } = error; + Promise.reject(new Error(response.data.error)); + }); } export function removeApiKey(keyId) { @@ -244,5 +267,8 @@ export function removeApiKey(keyId) { user: response.data }); }) - .catch(response => Promise.reject(new Error(response.data.error))); + .catch((error) => { + const { response } = error; + Promise.reject(new Error(response.data.error)); + }); }