review changes

This commit is contained in:
Hetu Nandu 2019-10-07 18:41:18 +05:30
parent 866181b340
commit db97974ed0
2 changed files with 22 additions and 38 deletions

View File

@ -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<T> {
type: ReduxActionType | ReduxActionErrorType;
payload: T;

View File

@ -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 });