Fix(axios error handling issue)

This commit is contained in:
shakti97 2020-04-25 20:18:39 +05:30
parent 33db5fb1ef
commit 15b3fa8312
7 changed files with 106 additions and 53 deletions

View file

@ -27,7 +27,8 @@ export function getCollections(username) {
}); });
dispatch(stopLoader()); dispatch(stopLoader());
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data
@ -57,7 +58,8 @@ export function createCollection(collection) {
browserHistory.push(location); browserHistory.push(location);
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
console.error('Error creating collection', response.data); console.error('Error creating collection', response.data);
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
@ -87,7 +89,8 @@ export function addToCollection(collectionId, projectId) {
return response.data; return response.data;
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data
@ -118,7 +121,8 @@ export function removeFromCollection(collectionId, projectId) {
return response.data; return response.data;
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data
@ -141,7 +145,8 @@ export function editCollection(collectionId, { name, description }) {
}); });
return response.data; return response.data;
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data
@ -164,7 +169,8 @@ export function deleteCollection(collectionId) {
}); });
return response.data; return response.data;
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data

View file

@ -65,10 +65,13 @@ export function createFile(formProps) {
// }); // });
dispatch(setUnsavedChanges(true)); dispatch(setUnsavedChanges(true));
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
error: response.data dispatch({
})); type: ActionTypes.ERROR,
error: response.data
});
});
} else { } else {
const id = objectID().toHexString(); const id = objectID().toHexString();
dispatch({ dispatch({
@ -113,10 +116,13 @@ export function createFolder(formProps) {
dispatch(setProjectSavedTime(response.data.project.updatedAt)); dispatch(setProjectSavedTime(response.data.project.updatedAt));
dispatch(closeNewFolderModal()); dispatch(closeNewFolderModal());
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
error: response.data dispatch({
})); type: ActionTypes.ERROR,
error: response.data
});
});
} else { } else {
const id = objectID().toHexString(); const id = objectID().toHexString();
dispatch({ dispatch({
@ -163,7 +169,8 @@ export function deleteFile(id, parentId) {
parentId parentId
}); });
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data

View file

@ -8,10 +8,13 @@ function updatePreferences(formParams, dispatch) {
axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true }) axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true })
.then(() => { .then(() => {
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
error: response.data dispatch({
})); type: ActionTypes.ERROR,
error: response.data
});
});
} }
export function setFontSize(value) { export function setFontSize(value) {

View file

@ -57,10 +57,13 @@ export function getProject(id) {
dispatch(setProject(response.data)); dispatch(setProject(response.data));
dispatch(setUnsavedChanges(false)); dispatch(setUnsavedChanges(false));
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
error: response.data 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()); dispatch(endSavingProject());
if (response.status === 403) { if (response.status === 403) {
dispatch(showErrorModal('staleSession')); dispatch(showErrorModal('staleSession'));
@ -200,7 +204,8 @@ export function saveProject(selectedFile = null, autosave = false) {
} }
} }
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch(endSavingProject()); dispatch(endSavingProject());
if (response.status === 403) { if (response.status === 403) {
dispatch(showErrorModal('staleSession')); dispatch(showErrorModal('staleSession'));
@ -298,10 +303,13 @@ export function cloneProject(id) {
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`); browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
dispatch(setNewProject(response.data)); dispatch(setNewProject(response.data));
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.PROJECT_SAVE_FAIL, const { response } = error;
error: response.data dispatch({
})); type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data
});
});
}); });
}); });
}; };
@ -344,8 +352,8 @@ export function changeProjectName(id, newName) {
} }
} }
}) })
.catch((response) => { .catch((error) => {
console.log(response); const { response } = error;
dispatch({ dispatch({
type: ActionTypes.PROJECT_SAVE_FAIL, type: ActionTypes.PROJECT_SAVE_FAIL,
error: response.data error: response.data
@ -368,7 +376,8 @@ export function deleteProject(id) {
id id
}); });
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
if (response.status === 403) { if (response.status === 403) {
dispatch(showErrorModal('staleSession')); dispatch(showErrorModal('staleSession'));
} else { } else {

View file

@ -23,7 +23,8 @@ export function getProjects(username) {
}); });
dispatch(stopLoader()); dispatch(stopLoader());
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
dispatch({ dispatch({
type: ActionTypes.ERROR, type: ActionTypes.ERROR,
error: response.data error: response.data

View file

@ -65,7 +65,8 @@ export function dropzoneAcceptCallback(userId, file, done) {
file.previewTemplate.className += ' uploading'; // eslint-disable-line file.previewTemplate.className += ' uploading'; // eslint-disable-line
done(); done();
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
file.custom_status = 'rejected'; // eslint-disable-line file.custom_status = 'rejected'; // eslint-disable-line
if (response.data && response.data.responseText && response.data.responseText.message) { if (response.data && response.data.responseText && response.data.responseText.message) {
done(response.data.responseText.message); done(response.data.responseText.message);

View file

@ -26,7 +26,10 @@ export function signUpUser(previousPath, formValues) {
dispatch(justOpenedProject()); dispatch(justOpenedProject());
browserHistory.push(previousPath); 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 preferences: response.data.preferences
}); });
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
const message = response.message || response.data.error; const message = response.message || response.data.error;
dispatch(authError(message)); dispatch(authError(message));
}); });
@ -98,7 +102,8 @@ export function validateSession() {
dispatch(showErrorModal('staleSession')); dispatch(showErrorModal('staleSession'));
} }
}) })
.catch((response) => { .catch((error) => {
const { response } = error;
if (response.status === 404) { if (response.status === 404) {
dispatch(showErrorModal('staleSession')); dispatch(showErrorModal('staleSession'));
} }
@ -114,7 +119,10 @@ export function logoutUser() {
type: ActionTypes.UNAUTH_USER 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(() => { .then(() => {
// do nothing // do nothing
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
message: response.data dispatch({
})); type: ActionTypes.ERROR,
message: response.data
});
});
}; };
} }
@ -143,10 +154,13 @@ export function initiateVerification() {
.then(() => { .then(() => {
// do nothing // do nothing
}) })
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.ERROR, const { response } = error;
message: response.data dispatch({
})); type: ActionTypes.ERROR,
message: response.data
});
});
}; };
} }
@ -161,10 +175,13 @@ export function verifyEmailConfirmation(token) {
type: ActionTypes.EMAIL_VERIFICATION_VERIFIED, type: ActionTypes.EMAIL_VERIFICATION_VERIFIED,
message: response.data, message: response.data,
})) }))
.catch(response => dispatch({ .catch((error) => {
type: ActionTypes.EMAIL_VERIFICATION_INVALID, const { response } = error;
message: response.data dispatch({
})); type: ActionTypes.EMAIL_VERIFICATION_INVALID,
message: response.data
});
});
}; };
} }
@ -216,7 +233,10 @@ export function updateSettings(formValues) {
dispatch(showToast(5500)); dispatch(showToast(5500));
dispatch(setToastText('Settings saved.')); 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) { export function createApiKeySuccess(user) {
@ -232,7 +252,10 @@ export function createApiKey(label) {
.then((response) => { .then((response) => {
dispatch(createApiKeySuccess(response.data)); 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) { export function removeApiKey(keyId) {
@ -244,5 +267,8 @@ export function removeApiKey(keyId) {
user: response.data user: response.data
}); });
}) })
.catch(response => Promise.reject(new Error(response.data.error))); .catch((error) => {
const { response } = error;
Promise.reject(new Error(response.data.error));
});
} }