review changes
This commit is contained in:
parent
866181b340
commit
db97974ed0
|
|
@ -1,6 +1,4 @@
|
||||||
import _ from "lodash";
|
|
||||||
import { WidgetProps, WidgetCardProps } from "../widgets/BaseWidget";
|
import { WidgetProps, WidgetCardProps } from "../widgets/BaseWidget";
|
||||||
import * as errorConstants from "./errors";
|
|
||||||
|
|
||||||
export const ReduxActionTypes: { [key: string]: string } = {
|
export const ReduxActionTypes: { [key: string]: string } = {
|
||||||
REPORT_ERROR: "REPORT_ERROR",
|
REPORT_ERROR: "REPORT_ERROR",
|
||||||
|
|
@ -54,35 +52,6 @@ export const ReduxActionErrorTypes: { [key: string]: string } = {
|
||||||
};
|
};
|
||||||
export type ReduxActionErrorType = (typeof ReduxActionErrorTypes)[keyof typeof ReduxActionErrorTypes];
|
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> {
|
export interface ReduxAction<T> {
|
||||||
type: ReduxActionType | ReduxActionErrorType;
|
type: ReduxActionType | ReduxActionErrorType;
|
||||||
payload: T;
|
payload: T;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
|
import _ from "lodash";
|
||||||
|
import { Intent } from "@blueprintjs/core";
|
||||||
import {
|
import {
|
||||||
ReduxActionTypes,
|
ReduxActionTypes,
|
||||||
ReduxActionErrorTypes,
|
ReduxActionErrorTypes,
|
||||||
ReduxAction,
|
ReduxAction,
|
||||||
ActionErrorDisplayMap,
|
|
||||||
} from "../constants/ReduxActionConstants";
|
} from "../constants/ReduxActionConstants";
|
||||||
|
|
||||||
import AppToaster from "../editorComponents/ToastComponent";
|
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 { ApiResponse } from "../api/ApiResponses";
|
||||||
import { put, takeLatest } from "redux-saga/effects";
|
import { put, takeLatest } from "redux-saga/effects";
|
||||||
|
|
||||||
|
|
@ -25,9 +27,22 @@ export function* validateResponse(response: ApiResponse) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* errorSaga(
|
type IError = object | { message: string };
|
||||||
errorAction: ReduxAction<{ error: { 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.
|
// Just a pass through for now.
|
||||||
// Add procedures to customize errors here
|
// Add procedures to customize errors here
|
||||||
console.log({ error: errorAction });
|
console.log({ error: errorAction });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user