53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { ThunkAction } from 'redux-thunk';
|
|
import { StoreState } from '@Reducers/_InitializeStore/types';
|
|
import { postMessageDialog } from '@Reducers/message/actions';
|
|
import { MessageDialogObject } from '@Models/Redux/Message/types';
|
|
import I18n from '@Models/Core/I18n';
|
|
import { APIError } from './types';
|
|
|
|
export const errorCatch = (
|
|
error: APIError,
|
|
actions?: () => void,
|
|
): ThunkAction<Promise<void>, StoreState, unknown, { type: string }> => async (dispatch, getState): Promise<void> => {
|
|
const storeGlobal = getState().global;
|
|
const env = storeGlobal.globalEnv;
|
|
const langs = storeGlobal.globalLang;
|
|
const i18n = new I18n(langs);
|
|
let messageHidden = '';
|
|
let messageTitle = i18n.t('frontend.global.error.0000');
|
|
let messageContent = '';
|
|
if (error && typeof error === 'object') {
|
|
if ('code' in error) {
|
|
if (i18n.t(`frontend.global.error.${error.code}`)) {
|
|
messageTitle = `${messageTitle}: (${error.code})`;
|
|
messageContent = i18n.t(`frontend.global.error.${error.code}`);
|
|
}
|
|
if (!i18n.t(`frontend.global.error.${error.code}`) && 'message' in error) {
|
|
messageContent = error.message;
|
|
}
|
|
if (env.EnvName !== 'production' && 'errorStack' in error) {
|
|
messageHidden += `\n${error.errorStack}`;
|
|
}
|
|
}
|
|
} else if (error && typeof error === 'string') {
|
|
messageTitle = i18n.t('frontend.global.error.0000');
|
|
messageContent = error;
|
|
} else {
|
|
messageContent = i18n.t('frontend.global.error.9999');
|
|
}
|
|
const data: MessageDialogObject = {
|
|
typeMessage: 'danger',
|
|
typeTitle: messageTitle,
|
|
typeContent: messageContent,
|
|
onConfirm: () => {
|
|
if (actions) {
|
|
actions();
|
|
}
|
|
},
|
|
};
|
|
if (messageHidden) {
|
|
data.typeHiddenMessage = messageHidden;
|
|
}
|
|
dispatch(postMessageDialog(data));
|
|
};
|