vben
2020-10-18 7101587b9676c91e9079044a096df08848f1f602
提交 | 用户 | age
2f6253 1 import store from '/@/store';
2 import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
710158 3 import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
2f6253 4
5 import { formatToDateTime } from '/@/utils/dateUtil';
710158 6 import { ErrorTypeEnum } from '/@/enums/exceptionEnum';
V 7 import { useSetting } from '/@/hooks/core/useSetting';
2f6253 8
9 export interface ErrorInfo {
10   type: ErrorTypeEnum;
11   file: string;
12   name?: string;
13   message: string;
14   stack?: string;
15   detail: string;
16   url: string;
17   time?: string;
18 }
19 export interface ErrorState {
20   errorInfoState: ErrorInfo[] | null;
21   errorListCountState: number;
22 }
23
24 const NAME = 'error';
25 hotModuleUnregisterModule(NAME);
26 @Module({ dynamic: true, namespaced: true, store, name: NAME })
27 class Error extends VuexModule implements ErrorState {
28   errorInfoState: ErrorInfo[] = [];
29   errorListCountState = 0;
30
31   get getErrorInfoState() {
32     return this.errorInfoState;
33   }
34
35   get getErrorListCountState() {
36     return this.errorListCountState;
37   }
38
39   @Mutation
40   commitErrorInfoState(info: ErrorInfo): void {
710158 41     const item = {
2f6253 42       ...info,
43       time: formatToDateTime(new Date()),
710158 44     };
V 45     this.errorInfoState = [item, ...this.errorInfoState];
2f6253 46     this.errorListCountState += 1;
47   }
48
49   @Mutation
50   commitErrorListCountState(count: number): void {
51     this.errorListCountState = count;
52   }
710158 53
V 54   @Action
55   setupErrorHandle(error: any) {
56     const { projectSetting } = useSetting();
57     const { useErrorHandle } = projectSetting;
58     if (!useErrorHandle) return;
59
60     const errInfo: Partial<ErrorInfo> = {
61       message: error.message,
62       type: ErrorTypeEnum.AJAX,
63     };
64     if (error.response) {
65       const {
66         config: { url = '', data: params = '', method = 'get', headers = {} } = {},
67         data = {},
68       } = error.response;
69       errInfo.url = url;
70       errInfo.name = 'Ajax Error!';
71       errInfo.file = '-';
72       errInfo.stack = JSON.stringify(data);
73       errInfo.detail = JSON.stringify({ params, method, headers });
74     }
75     this.commitErrorInfoState(errInfo as ErrorInfo);
76   }
2f6253 77 }
78 export { Error };
79 export const errorStore = getModule<Error>(Error);