From 0873e77ff4ec6aa24574bd117aa691cfc4e4acde Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Thu, 4 Mar 2021 13:57:00 +0700 Subject: [PATCH] fixed error handling --- src/redux/auth/sagas.ts | 18 +++++++++++------- src/redux/messages/sagas.ts | 8 ++++---- src/redux/node/sagas.ts | 4 ++-- src/redux/store.ts | 7 ++----- src/redux/uploads/sagas.ts | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/redux/auth/sagas.ts b/src/redux/auth/sagas.ts index 47e26fa0..8b3ba089 100644 --- a/src/redux/auth/sagas.ts +++ b/src/redux/auth/sagas.ts @@ -234,7 +234,7 @@ function* requestRestoreCode({ field }: ReturnType) { yield put(authSetRestore({ user: data.user, code, is_loading: false })); yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); } catch (error) { - yield put(authSetRestore({ is_loading: false, error: error || ERRORS.CODE_IS_INVALID })); + yield put( + authSetRestore({ is_loading: false, error: error.message || ERRORS.CODE_IS_INVALID }) + ); yield put(modalShowDialog(DIALOGS.RESTORE_PASSWORD)); } } @@ -276,7 +278,9 @@ function* restorePassword({ password }: ReturnType) yield call(refreshUser); } catch (error) { - return yield put(authSetRestore({ is_loading: false, error: error || ERRORS.CODE_IS_INVALID })); + return yield put( + authSetRestore({ is_loading: false, error: error.message || ERRORS.CODE_IS_INVALID }) + ); } } @@ -286,7 +290,7 @@ function* getSocials() { const data: Unwrap = yield call(apiGetSocials); yield put(authSetSocials({ accounts: data.accounts })); } catch (error) { - yield put(authSetSocials({ error })); + yield put(authSetSocials({ error: error.message })); } finally { yield put(authSetSocials({ is_loading: false })); } @@ -303,7 +307,7 @@ function* dropSocial({ provider, id }: ReturnType) { yield call(getSocials); } catch (error) { - yield put(authSetSocials({ error })); + yield put(authSetSocials({ error: error.message })); } } @@ -355,7 +359,7 @@ function* loginWithSocial({ token }: ReturnType) { return; } } catch (error) { - yield put(userSetLoginError(error)); + yield put(userSetLoginError(error.message)); } } @@ -400,7 +404,7 @@ function* authRegisterSocial({ username, password }: ReturnType) { } } catch (error) { messagesSet({ - error: error || ERRORS.EMPTY_RESPONSE, + error: error.message || ERRORS.EMPTY_RESPONSE, }); } finally { yield put( @@ -110,7 +110,7 @@ function* sendMessage({ message, onSuccess }: ReturnType) { yield put(messagesSet({ messages: newMessages })); } catch (error) { messagesSet({ - error: error || ERRORS.EMPTY_RESPONSE, + error: error.message || ERRORS.EMPTY_RESPONSE, }); } finally { yield put( diff --git a/src/redux/node/sagas.ts b/src/redux/node/sagas.ts index 15cf8909..fa077989 100644 --- a/src/redux/node/sagas.ts +++ b/src/redux/node/sagas.ts @@ -99,7 +99,7 @@ function* onNodeSave({ node }: ReturnType) { return yield put(modalSetShown(false)); } catch (error) { - yield put(nodeSetSaveErrors({ error: error || ERRORS.CANT_SAVE_NODE })); + yield put(nodeSetSaveErrors({ error: error.message || ERRORS.CANT_SAVE_NODE })); } } @@ -214,7 +214,7 @@ function* onPostComment({ nodeId, comment, callback }: ReturnType) => { + api.interceptors.response.use(undefined, (error: AxiosError<{ error: string }>) => { if (error.response?.status === 401) { store.dispatch(authLogout()); } - console.log('Вот что случилось на сервере:', error); - throw new Error( - error?.response?.data?.message || error?.message || error?.response?.statusText - ); + throw new Error(error?.response?.data?.error || error?.message || error?.response?.statusText); }); return { store, persistor }; diff --git a/src/redux/uploads/sagas.ts b/src/redux/uploads/sagas.ts index ff1b68a9..480bcf9a 100644 --- a/src/redux/uploads/sagas.ts +++ b/src/redux/uploads/sagas.ts @@ -133,7 +133,7 @@ function* uploadFile({ file, temp_id, type, target, onSuccess, onFail }: IFileWi return yield put( uploadSetStatus(temp_id, { is_uploading: false, - error, + error: error.message, type, }) );