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