2021-01-06 11:24:16 +00:00
|
|
|
import { get } from "lodash";
|
2019-09-27 16:05:33 +00:00
|
|
|
import {
|
|
|
|
|
ReduxActionTypes,
|
|
|
|
|
ReduxActionErrorTypes,
|
|
|
|
|
ReduxAction,
|
2019-11-25 05:07:27 +00:00
|
|
|
} from "constants/ReduxActionConstants";
|
2021-01-06 11:24:16 +00:00
|
|
|
import log from "loglevel";
|
|
|
|
|
import history from "utils/history";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { ApiResponse } from "api/ApiResponses";
|
2020-11-24 07:01:37 +00:00
|
|
|
import { Variant } from "components/ads/common";
|
|
|
|
|
import { Toaster } from "components/ads/Toast";
|
2020-12-17 07:03:59 +00:00
|
|
|
import { flushErrors } from "actions/errorActions";
|
2021-01-06 11:24:16 +00:00
|
|
|
import { AUTH_LOGIN_URL } from "constants/routes";
|
2021-03-04 05:24:47 +00:00
|
|
|
import { ERROR_CODES, SERVER_ERROR_CODES } from "constants/ApiConstants";
|
2020-12-17 07:03:59 +00:00
|
|
|
import { getSafeCrash } from "selectors/errorSelectors";
|
2021-01-06 11:24:16 +00:00
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
2021-01-11 05:28:10 +00:00
|
|
|
import { ANONYMOUS_USERNAME } from "constants/userConstants";
|
2021-01-06 11:24:16 +00:00
|
|
|
import { put, takeLatest, call, select } from "redux-saga/effects";
|
2021-01-21 06:42:53 +00:00
|
|
|
import {
|
|
|
|
|
ERROR_401,
|
|
|
|
|
ERROR_500,
|
|
|
|
|
ERROR_0,
|
|
|
|
|
DEFAULT_ERROR_MESSAGE,
|
|
|
|
|
} from "constants/messages";
|
2019-09-27 16:05:33 +00:00
|
|
|
|
2021-01-21 11:08:18 +00:00
|
|
|
/**
|
|
|
|
|
* making with error message with action name
|
|
|
|
|
*
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
export const getDefaultActionError = (action: string) =>
|
|
|
|
|
`Incurred an error when ${action}`;
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
export function* callAPI(apiCall: any, requestPayload: any) {
|
|
|
|
|
try {
|
|
|
|
|
return yield call(apiCall, requestPayload);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return yield error;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-21 11:08:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* transforn server errors to client error codes
|
|
|
|
|
*
|
|
|
|
|
* @param code
|
|
|
|
|
*/
|
2019-12-16 08:49:10 +00:00
|
|
|
const getErrorMessage = (code: number) => {
|
|
|
|
|
switch (code) {
|
2020-04-08 08:43:56 +00:00
|
|
|
case 401:
|
|
|
|
|
return ERROR_401;
|
2019-12-16 08:49:10 +00:00
|
|
|
case 500:
|
|
|
|
|
return ERROR_500;
|
2020-04-05 06:34:14 +00:00
|
|
|
case 0:
|
|
|
|
|
return ERROR_0;
|
2019-12-16 08:49:10 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-04 05:24:47 +00:00
|
|
|
export class IncorrectBindingError extends Error {}
|
|
|
|
|
|
2021-01-21 11:08:18 +00:00
|
|
|
/**
|
|
|
|
|
* validates if response does have any errors
|
|
|
|
|
*
|
|
|
|
|
* @param response
|
|
|
|
|
* @param show
|
|
|
|
|
*/
|
2020-10-07 15:19:30 +00:00
|
|
|
export function* validateResponse(response: ApiResponse | any, show = true) {
|
2020-04-08 08:43:56 +00:00
|
|
|
if (!response) {
|
|
|
|
|
throw Error("");
|
|
|
|
|
}
|
2020-04-05 06:34:14 +00:00
|
|
|
if (!response.responseMeta && !response.status) {
|
|
|
|
|
throw Error(getErrorMessage(0));
|
|
|
|
|
}
|
2019-12-16 08:49:10 +00:00
|
|
|
if (!response.responseMeta && response.status) {
|
|
|
|
|
throw Error(getErrorMessage(response.status));
|
|
|
|
|
}
|
2019-09-27 16:05:33 +00:00
|
|
|
if (response.responseMeta.success) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
2021-03-04 05:24:47 +00:00
|
|
|
if (
|
|
|
|
|
response.responseMeta.error.code ===
|
|
|
|
|
SERVER_ERROR_CODES.INCORRECT_BINDING_LIST_OF_WIDGET
|
|
|
|
|
) {
|
|
|
|
|
throw new IncorrectBindingError(response.responseMeta.error.message);
|
|
|
|
|
} else {
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionErrorTypes.API_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
error: response.responseMeta.error,
|
|
|
|
|
show,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
throw Error(response.responseMeta.error.message);
|
|
|
|
|
}
|
2019-09-27 16:05:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
export function getResponseErrorMessage(response: ApiResponse) {
|
|
|
|
|
return response.responseMeta.error
|
|
|
|
|
? response.responseMeta.error.message
|
|
|
|
|
: undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
type ErrorPayloadType = {
|
|
|
|
|
code?: number | string;
|
|
|
|
|
message?: string;
|
|
|
|
|
crash?: boolean;
|
|
|
|
|
};
|
2021-01-21 06:42:53 +00:00
|
|
|
const ActionErrorDisplayMap: {
|
2019-10-07 13:19:29 +00:00
|
|
|
[key: string]: (error: ErrorPayloadType) => string;
|
2021-01-21 06:42:53 +00:00
|
|
|
} = {
|
2020-12-24 04:32:25 +00:00
|
|
|
[ReduxActionErrorTypes.API_ERROR]: (error) =>
|
2021-01-06 11:24:16 +00:00
|
|
|
get(error, "message", DEFAULT_ERROR_MESSAGE),
|
2019-10-07 13:11:18 +00:00
|
|
|
[ReduxActionErrorTypes.FETCH_PAGE_ERROR]: () =>
|
2021-01-21 11:08:18 +00:00
|
|
|
getDefaultActionError("fetching the page"),
|
2019-10-07 13:11:18 +00:00
|
|
|
[ReduxActionErrorTypes.SAVE_PAGE_ERROR]: () =>
|
2021-01-21 11:08:18 +00:00
|
|
|
getDefaultActionError("saving the page"),
|
2019-10-07 13:11:18 +00:00
|
|
|
};
|
|
|
|
|
|
2021-01-21 06:42:53 +00:00
|
|
|
const getErrorMessageFromActionType = (
|
|
|
|
|
type: string,
|
|
|
|
|
error: ErrorPayloadType,
|
|
|
|
|
): string => {
|
|
|
|
|
const actionErrorMessage = get(error, "message");
|
|
|
|
|
if (actionErrorMessage === undefined) {
|
|
|
|
|
if (type in ActionErrorDisplayMap) {
|
|
|
|
|
return ActionErrorDisplayMap[type](error);
|
|
|
|
|
}
|
|
|
|
|
return DEFAULT_ERROR_MESSAGE;
|
|
|
|
|
}
|
|
|
|
|
return actionErrorMessage;
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
enum ErrorEffectTypes {
|
|
|
|
|
SHOW_ALERT = "SHOW_ALERT",
|
|
|
|
|
SAFE_CRASH = "SAFE_CRASH",
|
|
|
|
|
LOG_ERROR = "LOG_ERROR",
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 09:38:25 +00:00
|
|
|
export interface ErrorActionPayload {
|
|
|
|
|
error: ErrorPayloadType;
|
|
|
|
|
show?: boolean;
|
|
|
|
|
crash?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function* errorSaga(errorAction: ReduxAction<ErrorActionPayload>) {
|
2020-12-08 19:13:48 +00:00
|
|
|
const effects = [ErrorEffectTypes.LOG_ERROR];
|
2021-01-05 06:42:04 +00:00
|
|
|
const { type, payload } = errorAction;
|
|
|
|
|
const { show = true, error } = payload || {};
|
2021-01-21 06:42:53 +00:00
|
|
|
const message = getErrorMessageFromActionType(type, error);
|
2020-07-15 07:19:52 +00:00
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
if (show) {
|
|
|
|
|
effects.push(ErrorEffectTypes.SHOW_ALERT);
|
|
|
|
|
}
|
2021-01-11 05:28:10 +00:00
|
|
|
|
2020-12-09 12:52:36 +00:00
|
|
|
if (error && error.crash) {
|
2020-12-08 19:13:48 +00:00
|
|
|
effects.push(ErrorEffectTypes.SAFE_CRASH);
|
|
|
|
|
}
|
2021-01-06 11:24:16 +00:00
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
for (const effect of effects) {
|
|
|
|
|
switch (effect) {
|
|
|
|
|
case ErrorEffectTypes.LOG_ERROR: {
|
|
|
|
|
logErrorSaga(errorAction);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ErrorEffectTypes.SHOW_ALERT: {
|
|
|
|
|
showAlertAboutError(message);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ErrorEffectTypes.SAFE_CRASH: {
|
2021-03-04 05:24:47 +00:00
|
|
|
yield call(crashAppSaga, error);
|
2020-12-08 19:13:48 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-15 14:19:39 +00:00
|
|
|
}
|
2021-01-06 11:24:16 +00:00
|
|
|
|
2019-09-27 16:05:33 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.REPORT_ERROR,
|
|
|
|
|
payload: {
|
|
|
|
|
source: errorAction.type,
|
2020-12-08 19:13:48 +00:00
|
|
|
message,
|
2019-09-27 16:05:33 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-08 19:13:48 +00:00
|
|
|
function logErrorSaga(action: ReduxAction<{ error: ErrorPayloadType }>) {
|
|
|
|
|
log.debug(`Error in action ${action.type}`);
|
2020-12-09 07:04:08 +00:00
|
|
|
if (action.payload) log.error(action.payload.error);
|
2020-12-08 19:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showAlertAboutError(message: string) {
|
|
|
|
|
Toaster.show({ text: message, variant: Variant.danger });
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 05:24:47 +00:00
|
|
|
function* crashAppSaga(error: ErrorPayloadType) {
|
2020-12-08 19:13:48 +00:00
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SAFE_CRASH_APPSMITH,
|
2021-03-04 05:24:47 +00:00
|
|
|
payload: error,
|
2020-12-08 19:13:48 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 11:24:16 +00:00
|
|
|
/**
|
|
|
|
|
* this saga do some logic before actually setting safeCrash to true
|
|
|
|
|
*/
|
|
|
|
|
function* safeCrashSagaRequest(action: ReduxAction<{ code?: string }>) {
|
|
|
|
|
const user = yield select(getCurrentUser);
|
|
|
|
|
const code = get(action, "payload.code");
|
|
|
|
|
|
|
|
|
|
// if user is not logged and the error is "PAGE_NOT_FOUND",
|
|
|
|
|
// redirecting user to login page with redirecTo param
|
|
|
|
|
if (
|
2021-01-11 05:28:10 +00:00
|
|
|
get(user, "email") === ANONYMOUS_USERNAME &&
|
2021-01-06 11:24:16 +00:00
|
|
|
code === ERROR_CODES.PAGE_NOT_FOUND
|
|
|
|
|
) {
|
|
|
|
|
window.location.href = `${AUTH_LOGIN_URL}?redirectUrl=${window.location.href}`;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if there is no action to be done, just calling the safe crash action
|
|
|
|
|
yield put({
|
|
|
|
|
type: ReduxActionTypes.SAFE_CRASH_APPSMITH,
|
|
|
|
|
payload: {
|
|
|
|
|
code,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 07:03:59 +00:00
|
|
|
/**
|
|
|
|
|
* flush errors and redirect users to a url
|
|
|
|
|
*
|
|
|
|
|
* @param action
|
|
|
|
|
*/
|
|
|
|
|
export function* flushErrorsAndRedirectSaga(
|
|
|
|
|
action: ReduxAction<{ url?: string }>,
|
|
|
|
|
) {
|
|
|
|
|
const safeCrash = yield select(getSafeCrash);
|
|
|
|
|
|
|
|
|
|
if (safeCrash) {
|
|
|
|
|
yield put(flushErrors());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
history.push(action.payload.url);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 16:05:33 +00:00
|
|
|
export default function* errorSagas() {
|
|
|
|
|
yield takeLatest(Object.values(ReduxActionErrorTypes), errorSaga);
|
2020-12-17 07:03:59 +00:00
|
|
|
yield takeLatest(
|
|
|
|
|
ReduxActionTypes.FLUSH_AND_REDIRECT,
|
|
|
|
|
flushErrorsAndRedirectSaga,
|
|
|
|
|
);
|
2021-01-06 11:24:16 +00:00
|
|
|
yield takeLatest(
|
|
|
|
|
ReduxActionTypes.SAFE_CRASH_APPSMITH_REQUEST,
|
|
|
|
|
safeCrashSagaRequest,
|
|
|
|
|
);
|
2019-09-27 16:05:33 +00:00
|
|
|
}
|