From db97974ed0a9f757b57d9caea44ab8784cbd9e8d Mon Sep 17 00:00:00 2001 From: Hetu Nandu Date: Mon, 7 Oct 2019 18:41:18 +0530 Subject: [PATCH] review changes --- .../src/constants/ReduxActionConstants.tsx | 31 ------------------- app/client/src/sagas/ErrorSagas.tsx | 29 ++++++++++++----- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/client/src/constants/ReduxActionConstants.tsx b/app/client/src/constants/ReduxActionConstants.tsx index 1e72503a14..a40adcf4e7 100644 --- a/app/client/src/constants/ReduxActionConstants.tsx +++ b/app/client/src/constants/ReduxActionConstants.tsx @@ -1,6 +1,4 @@ -import _ from "lodash"; import { WidgetProps, WidgetCardProps } from "../widgets/BaseWidget"; -import * as errorConstants from "./errors"; export const ReduxActionTypes: { [key: string]: string } = { REPORT_ERROR: "REPORT_ERROR", @@ -54,35 +52,6 @@ export const ReduxActionErrorTypes: { [key: string]: string } = { }; export type ReduxActionErrorType = (typeof ReduxActionErrorTypes)[keyof typeof ReduxActionErrorTypes]; -const apiErrorHandler = (error: object, fallbackMessage: string) => { - const apiError = _.get(error, "response.data.responseMeta.error"); - if (errorConstants.API_ERROR_CODES.indexOf(apiError.code) > -1) { - return apiError.message; - } - return fallbackMessage; -}; - -export const ActionErrorDisplayMap: { - [key: string]: (error: object) => string; -} = { - [ReduxActionErrorTypes.API_ERROR]: error => - apiErrorHandler(error, errorConstants.DEFAULT_ERROR_MESSAGE), - [ReduxActionErrorTypes.FETCH_PAGE_ERROR]: error => - apiErrorHandler( - error, - errorConstants.DEFAULT_ACTION_ERROR("fetching the page"), - ), - [ReduxActionErrorTypes.SAVE_PAGE_ERROR]: error => - apiErrorHandler( - error, - errorConstants.DEFAULT_ACTION_ERROR("saving the page"), - ), - [ReduxActionErrorTypes.FETCH_WIDGET_CARDS_ERROR]: () => - errorConstants.DEFAULT_ERROR_MESSAGE, - [ReduxActionErrorTypes.WIDGET_OPERATION_ERROR]: () => - errorConstants.DEFAULT_ERROR_MESSAGE, -}; - export interface ReduxAction { type: ReduxActionType | ReduxActionErrorType; payload: T; diff --git a/app/client/src/sagas/ErrorSagas.tsx b/app/client/src/sagas/ErrorSagas.tsx index d868a64b69..832f7b0706 100644 --- a/app/client/src/sagas/ErrorSagas.tsx +++ b/app/client/src/sagas/ErrorSagas.tsx @@ -1,13 +1,15 @@ +import _ from "lodash"; +import { Intent } from "@blueprintjs/core"; import { ReduxActionTypes, ReduxActionErrorTypes, ReduxAction, - ActionErrorDisplayMap, } from "../constants/ReduxActionConstants"; - import AppToaster from "../editorComponents/ToastComponent"; -import { Intent } from "@blueprintjs/core"; - +import { + DEFAULT_ERROR_MESSAGE, + DEFAULT_ACTION_ERROR, +} from "../constants/errors"; import { ApiResponse } from "../api/ApiResponses"; import { put, takeLatest } from "redux-saga/effects"; @@ -25,9 +27,22 @@ export function* validateResponse(response: ApiResponse) { } } -export function* errorSaga( - errorAction: ReduxAction<{ error: { message: string } }>, -) { +type IError = object | { message: string }; + +const ActionErrorDisplayMap: { + [key: string]: (error: IError) => string; +} = { + [ReduxActionErrorTypes.API_ERROR]: error => + _.get(error, "message", DEFAULT_ERROR_MESSAGE), + [ReduxActionErrorTypes.FETCH_PAGE_ERROR]: () => + DEFAULT_ACTION_ERROR("fetching the page"), + [ReduxActionErrorTypes.SAVE_PAGE_ERROR]: () => + DEFAULT_ACTION_ERROR("saving the page"), + [ReduxActionErrorTypes.FETCH_WIDGET_CARDS_ERROR]: () => DEFAULT_ERROR_MESSAGE, + [ReduxActionErrorTypes.WIDGET_OPERATION_ERROR]: () => DEFAULT_ERROR_MESSAGE, +}; + +export function* errorSaga(errorAction: ReduxAction<{ error: IError }>) { // Just a pass through for now. // Add procedures to customize errors here console.log({ error: errorAction });