DEV;Add global onError to middleware
This commit is contained in:
parent
d6fbfddc6d
commit
9548d95d64
2 changed files with 11 additions and 7 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { Middleware } from 'redux';
|
import { AnyAction, Middleware } from 'redux';
|
||||||
import { is } from 'ramda';
|
import { is } from 'ramda';
|
||||||
|
|
||||||
import { IAction } from '@romanjaros/treejs-types/redux/actions';
|
import { IAction } from '@romanjaros/treejs-types/redux/actions';
|
||||||
|
@ -11,6 +11,7 @@ import { resolveParams } from './utils';
|
||||||
|
|
||||||
interface ISettings {
|
interface ISettings {
|
||||||
BASE_ENDPOINT_URL: string;
|
BASE_ENDPOINT_URL: string;
|
||||||
|
onError?: (error: any, setting: ISettings) => AnyAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const apiMiddleware = (setting: ISettings): Middleware => (store) => (next) => async (
|
export const apiMiddleware = (setting: ISettings): Middleware => (store) => (next) => async (
|
||||||
|
@ -36,21 +37,23 @@ export const apiMiddleware = (setting: ISettings): Middleware => (store) => (nex
|
||||||
body: JSON.stringify(action.payload.body),
|
body: JSON.stringify(action.payload.body),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(response.statusText);
|
|
||||||
}
|
|
||||||
|
|
||||||
store.dispatch(fetchStatusActions.onSuccess(action.payload));
|
|
||||||
|
|
||||||
const json = await response.text().then((text: string) => {
|
const json = await response.text().then((text: string) => {
|
||||||
return isJSON(text) ? JSON.parse(text) : text;
|
return isJSON(text) ? JSON.parse(text) : text;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw json;
|
||||||
|
}
|
||||||
|
|
||||||
|
store.dispatch(fetchStatusActions.onSuccess(action.payload));
|
||||||
if (is(Function, action.payload.onSuccess)) {
|
if (is(Function, action.payload.onSuccess)) {
|
||||||
await action.payload.onSuccess(json);
|
await action.payload.onSuccess(json);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
store.dispatch(fetchStatusActions.onError(action.payload));
|
store.dispatch(fetchStatusActions.onError(action.payload));
|
||||||
|
if (is(Function, setting.onError)) {
|
||||||
|
store.dispatch(setting.onError(error, setting));
|
||||||
|
}
|
||||||
if (is(Function, action.payload.onError)) {
|
if (is(Function, action.payload.onError)) {
|
||||||
action.payload.onError(error);
|
action.payload.onError(error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
"eslint": "eslint -c .eslintrc modules/**/*.{ts,tsx}",
|
"eslint": "eslint -c .eslintrc modules/**/*.{ts,tsx}",
|
||||||
"stylelint": "stylelint modules/**/*.ts",
|
"stylelint": "stylelint modules/**/*.ts",
|
||||||
"prettier": "prettier --check modules/**/*.ts",
|
"prettier": "prettier --check modules/**/*.ts",
|
||||||
|
"prettier:fix": "prettier --check --write modules/**/*.ts",
|
||||||
"lint": "npm run eslint && npm run stylelint && npm run prettier",
|
"lint": "npm run eslint && npm run stylelint && npm run prettier",
|
||||||
"build:doc": "node genDoc.js",
|
"build:doc": "node genDoc.js",
|
||||||
"build:app": "lerna run --stream --scope documentation build",
|
"build:app": "lerna run --stream --scope documentation build",
|
||||||
|
|
Loading…
Add table
Reference in a new issue