From f3cccd9aee495f6141f058d1d1ab1a2f5cac5aed Mon Sep 17 00:00:00 2001 From: Abhinav Jha Date: Wed, 15 Apr 2020 19:49:39 +0530 Subject: [PATCH] Remove errors when 401 unauthorized. --- app/client/src/api/Api.tsx | 9 +++++++-- app/client/src/sagas/ErrorSagas.tsx | 9 +++++++-- app/client/src/sagas/userSagas.tsx | 4 ++++ app/client/src/utils/AnalyticsUtil.tsx | 4 ++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/client/src/api/Api.tsx b/app/client/src/api/Api.tsx index a737ea47ee..9d02349f9b 100644 --- a/app/client/src/api/Api.tsx +++ b/app/client/src/api/Api.tsx @@ -8,6 +8,7 @@ import { import { ActionApiResponse } from "./ActionAPI"; import { AUTH_LOGIN_URL } from "constants/routes"; import { setRouteBeforeLogin } from "utils/storage"; +import history from "utils/history"; const { apiUrl, baseUrl } = getAppsmithConfigs(); //TODO(abhinav): Refactor this to make more composable. @@ -58,8 +59,12 @@ axiosInstance.interceptors.response.use( // console.log(error.response.headers); if (error.response.status === 401) { setRouteBeforeLogin(window.location.pathname); - window.location.href = AUTH_LOGIN_URL; - return; + history.push(AUTH_LOGIN_URL); + return Promise.reject({ + code: 401, + message: "Unauthorized. Redirecting to login page...", + show: false, + }); } if (error.response.data.responseMeta) { return Promise.resolve(error.response.data); diff --git a/app/client/src/sagas/ErrorSagas.tsx b/app/client/src/sagas/ErrorSagas.tsx index bd117ed732..d9ff92066e 100644 --- a/app/client/src/sagas/ErrorSagas.tsx +++ b/app/client/src/sagas/ErrorSagas.tsx @@ -59,7 +59,7 @@ export function getResponseErrorMessage(response: ApiResponse) { : undefined; } -type ErrorPayloadType = { message?: string }; +type ErrorPayloadType = { code?: number; message?: string }; let ActionErrorDisplayMap: { [key: string]: (error: ErrorPayloadType) => string; } = {}; @@ -92,7 +92,12 @@ export function* errorSaga( } = errorAction; const message = error && error.message ? error.message : ActionErrorDisplayMap[type](error); - if (show) AppToaster.show({ message, type: ToastType.ERROR }); + if (show && error.code !== 401) { + // error.code !== 401 IS A HACK! + // TODO(abhinav): Figure out a generic way + AppToaster.show({ message, type: ToastType.ERROR }); + } + yield put({ type: ReduxActionTypes.REPORT_ERROR, payload: { diff --git a/app/client/src/sagas/userSagas.tsx b/app/client/src/sagas/userSagas.tsx index 250dabcc5d..c4cabaec48 100644 --- a/app/client/src/sagas/userSagas.tsx +++ b/app/client/src/sagas/userSagas.tsx @@ -288,6 +288,10 @@ export function* fetchUserSaga(action: ReduxAction) { return yield false; } catch (error) { console.log(error); + yield put({ + type: ReduxActionErrorTypes.FETCH_USER_ERROR, + payload: error, + }); } } diff --git a/app/client/src/utils/AnalyticsUtil.tsx b/app/client/src/utils/AnalyticsUtil.tsx index 1d81f92940..c458a6b5d0 100644 --- a/app/client/src/utils/AnalyticsUtil.tsx +++ b/app/client/src/utils/AnalyticsUtil.tsx @@ -184,8 +184,8 @@ class AnalyticsUtil { static reset() { const windowDoc: any = window; - windowDoc.analytics.reset(); - windowDoc.mixpanel.reset(); + windowDoc.analytics && windowDoc.analytics.reset(); + windowDoc.mixpanel && windowDoc.mixpanel.reset(); } }