Skip to content

Commit 45b9016

Browse files
authored
Merge pull request #1404 from shakti97/axios-error-handling
Fix(axios error handling issue)
2 parents 4f1b9e3 + 15b3fa8 commit 45b9016

File tree

7 files changed

+106
-53
lines changed

7 files changed

+106
-53
lines changed

client/modules/IDE/actions/collections.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export function getCollections(username) {
2727
});
2828
dispatch(stopLoader());
2929
})
30-
.catch((response) => {
30+
.catch((error) => {
31+
const { response } = error;
3132
dispatch({
3233
type: ActionTypes.ERROR,
3334
error: response.data
@@ -57,7 +58,8 @@ export function createCollection(collection) {
5758

5859
browserHistory.push(location);
5960
})
60-
.catch((response) => {
61+
.catch((error) => {
62+
const { response } = error;
6163
console.error('Error creating collection', response.data);
6264
dispatch({
6365
type: ActionTypes.ERROR,
@@ -87,7 +89,8 @@ export function addToCollection(collectionId, projectId) {
8789

8890
return response.data;
8991
})
90-
.catch((response) => {
92+
.catch((error) => {
93+
const { response } = error;
9194
dispatch({
9295
type: ActionTypes.ERROR,
9396
error: response.data
@@ -118,7 +121,8 @@ export function removeFromCollection(collectionId, projectId) {
118121

119122
return response.data;
120123
})
121-
.catch((response) => {
124+
.catch((error) => {
125+
const { response } = error;
122126
dispatch({
123127
type: ActionTypes.ERROR,
124128
error: response.data
@@ -141,7 +145,8 @@ export function editCollection(collectionId, { name, description }) {
141145
});
142146
return response.data;
143147
})
144-
.catch((response) => {
148+
.catch((error) => {
149+
const { response } = error;
145150
dispatch({
146151
type: ActionTypes.ERROR,
147152
error: response.data
@@ -164,7 +169,8 @@ export function deleteCollection(collectionId) {
164169
});
165170
return response.data;
166171
})
167-
.catch((response) => {
172+
.catch((error) => {
173+
const { response } = error;
168174
dispatch({
169175
type: ActionTypes.ERROR,
170176
error: response.data

client/modules/IDE/actions/files.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ export function createFile(formProps) {
6565
// });
6666
dispatch(setUnsavedChanges(true));
6767
})
68-
.catch(response => dispatch({
69-
type: ActionTypes.ERROR,
70-
error: response.data
71-
}));
68+
.catch((error) => {
69+
const { response } = error;
70+
dispatch({
71+
type: ActionTypes.ERROR,
72+
error: response.data
73+
});
74+
});
7275
} else {
7376
const id = objectID().toHexString();
7477
dispatch({
@@ -113,10 +116,13 @@ export function createFolder(formProps) {
113116
dispatch(setProjectSavedTime(response.data.project.updatedAt));
114117
dispatch(closeNewFolderModal());
115118
})
116-
.catch(response => dispatch({
117-
type: ActionTypes.ERROR,
118-
error: response.data
119-
}));
119+
.catch((error) => {
120+
const { response } = error;
121+
dispatch({
122+
type: ActionTypes.ERROR,
123+
error: response.data
124+
});
125+
});
120126
} else {
121127
const id = objectID().toHexString();
122128
dispatch({
@@ -163,7 +169,8 @@ export function deleteFile(id, parentId) {
163169
parentId
164170
});
165171
})
166-
.catch((response) => {
172+
.catch((error) => {
173+
const { response } = error;
167174
dispatch({
168175
type: ActionTypes.ERROR,
169176
error: response.data

client/modules/IDE/actions/preferences.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ function updatePreferences(formParams, dispatch) {
88
axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true })
99
.then(() => {
1010
})
11-
.catch(response => dispatch({
12-
type: ActionTypes.ERROR,
13-
error: response.data
14-
}));
11+
.catch((error) => {
12+
const { response } = error;
13+
dispatch({
14+
type: ActionTypes.ERROR,
15+
error: response.data
16+
});
17+
});
1518
}
1619

1720
export function setFontSize(value) {

client/modules/IDE/actions/project.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ export function getProject(id) {
5757
dispatch(setProject(response.data));
5858
dispatch(setUnsavedChanges(false));
5959
})
60-
.catch(response => dispatch({
61-
type: ActionTypes.ERROR,
62-
error: response.data
63-
}));
60+
.catch((error) => {
61+
const { response } = error;
62+
dispatch({
63+
type: ActionTypes.ERROR,
64+
error: response.data
65+
});
66+
});
6467
};
6568
}
6669

@@ -161,7 +164,8 @@ export function saveProject(selectedFile = null, autosave = false) {
161164
}
162165
}
163166
})
164-
.catch((response) => {
167+
.catch((error) => {
168+
const { response } = error;
165169
dispatch(endSavingProject());
166170
if (response.status === 403) {
167171
dispatch(showErrorModal('staleSession'));
@@ -200,7 +204,8 @@ export function saveProject(selectedFile = null, autosave = false) {
200204
}
201205
}
202206
})
203-
.catch((response) => {
207+
.catch((error) => {
208+
const { response } = error;
204209
dispatch(endSavingProject());
205210
if (response.status === 403) {
206211
dispatch(showErrorModal('staleSession'));
@@ -298,10 +303,13 @@ export function cloneProject(id) {
298303
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
299304
dispatch(setNewProject(response.data));
300305
})
301-
.catch(response => dispatch({
302-
type: ActionTypes.PROJECT_SAVE_FAIL,
303-
error: response.data
304-
}));
306+
.catch((error) => {
307+
const { response } = error;
308+
dispatch({
309+
type: ActionTypes.PROJECT_SAVE_FAIL,
310+
error: response.data
311+
});
312+
});
305313
});
306314
});
307315
};
@@ -344,8 +352,8 @@ export function changeProjectName(id, newName) {
344352
}
345353
}
346354
})
347-
.catch((response) => {
348-
console.log(response);
355+
.catch((error) => {
356+
const { response } = error;
349357
dispatch({
350358
type: ActionTypes.PROJECT_SAVE_FAIL,
351359
error: response.data
@@ -368,7 +376,8 @@ export function deleteProject(id) {
368376
id
369377
});
370378
})
371-
.catch((response) => {
379+
.catch((error) => {
380+
const { response } = error;
372381
if (response.status === 403) {
373382
dispatch(showErrorModal('staleSession'));
374383
} else {

client/modules/IDE/actions/projects.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export function getProjects(username) {
2323
});
2424
dispatch(stopLoader());
2525
})
26-
.catch((response) => {
26+
.catch((error) => {
27+
const { response } = error;
2728
dispatch({
2829
type: ActionTypes.ERROR,
2930
error: response.data

client/modules/IDE/actions/uploader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export function dropzoneAcceptCallback(userId, file, done) {
6565
file.previewTemplate.className += ' uploading'; // eslint-disable-line
6666
done();
6767
})
68-
.catch((response) => {
68+
.catch((error) => {
69+
const { response } = error;
6970
file.custom_status = 'rejected'; // eslint-disable-line
7071
if (response.data && response.data.responseText && response.data.responseText.message) {
7172
done(response.data.responseText.message);

client/modules/User/actions.js

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ export function signUpUser(previousPath, formValues) {
2626
dispatch(justOpenedProject());
2727
browserHistory.push(previousPath);
2828
})
29-
.catch(response => dispatch(authError(response.data.error)));
29+
.catch((error) => {
30+
const { response } = error;
31+
dispatch(authError(response.data.error));
32+
});
3033
};
3134
}
3235

@@ -82,7 +85,8 @@ export function getUser() {
8285
preferences: response.data.preferences
8386
});
8487
})
85-
.catch((response) => {
88+
.catch((error) => {
89+
const { response } = error;
8690
const message = response.message || response.data.error;
8791
dispatch(authError(message));
8892
});
@@ -98,7 +102,8 @@ export function validateSession() {
98102
dispatch(showErrorModal('staleSession'));
99103
}
100104
})
101-
.catch((response) => {
105+
.catch((error) => {
106+
const { response } = error;
102107
if (response.status === 404) {
103108
dispatch(showErrorModal('staleSession'));
104109
}
@@ -114,7 +119,10 @@ export function logoutUser() {
114119
type: ActionTypes.UNAUTH_USER
115120
});
116121
})
117-
.catch(response => dispatch(authError(response.data.error)));
122+
.catch((error) => {
123+
const { response } = error;
124+
dispatch(authError(response.data.error));
125+
});
118126
};
119127
}
120128

@@ -127,10 +135,13 @@ export function initiateResetPassword(formValues) {
127135
.then(() => {
128136
// do nothing
129137
})
130-
.catch(response => dispatch({
131-
type: ActionTypes.ERROR,
132-
message: response.data
133-
}));
138+
.catch((error) => {
139+
const { response } = error;
140+
dispatch({
141+
type: ActionTypes.ERROR,
142+
message: response.data
143+
});
144+
});
134145
};
135146
}
136147

@@ -143,10 +154,13 @@ export function initiateVerification() {
143154
.then(() => {
144155
// do nothing
145156
})
146-
.catch(response => dispatch({
147-
type: ActionTypes.ERROR,
148-
message: response.data
149-
}));
157+
.catch((error) => {
158+
const { response } = error;
159+
dispatch({
160+
type: ActionTypes.ERROR,
161+
message: response.data
162+
});
163+
});
150164
};
151165
}
152166

@@ -161,10 +175,13 @@ export function verifyEmailConfirmation(token) {
161175
type: ActionTypes.EMAIL_VERIFICATION_VERIFIED,
162176
message: response.data,
163177
}))
164-
.catch(response => dispatch({
165-
type: ActionTypes.EMAIL_VERIFICATION_INVALID,
166-
message: response.data
167-
}));
178+
.catch((error) => {
179+
const { response } = error;
180+
dispatch({
181+
type: ActionTypes.EMAIL_VERIFICATION_INVALID,
182+
message: response.data
183+
});
184+
});
168185
};
169186
}
170187

@@ -216,7 +233,10 @@ export function updateSettings(formValues) {
216233
dispatch(showToast(5500));
217234
dispatch(setToastText('Settings saved.'));
218235
})
219-
.catch(response => Promise.reject(new Error(response.data.error)));
236+
.catch((error) => {
237+
const { response } = error;
238+
Promise.reject(new Error(response.data.error));
239+
});
220240
}
221241

222242
export function createApiKeySuccess(user) {
@@ -232,7 +252,10 @@ export function createApiKey(label) {
232252
.then((response) => {
233253
dispatch(createApiKeySuccess(response.data));
234254
})
235-
.catch(response => Promise.reject(new Error(response.data.error)));
255+
.catch((error) => {
256+
const { response } = error;
257+
Promise.reject(new Error(response.data.error));
258+
});
236259
}
237260

238261
export function removeApiKey(keyId) {
@@ -244,5 +267,8 @@ export function removeApiKey(keyId) {
244267
user: response.data
245268
});
246269
})
247-
.catch(response => Promise.reject(new Error(response.data.error)));
270+
.catch((error) => {
271+
const { response } = error;
272+
Promise.reject(new Error(response.data.error));
273+
});
248274
}

0 commit comments

Comments
 (0)