-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator.ts
101 lines (91 loc) · 2.43 KB
/
decorator.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// 定义各种各样的错误类型
class DbError extends Error {
public errmsg: string;
public errno: number;
constructor(msg: string, code: number) {
super(msg);
this.errmsg = msg || 'db_error_msg';
this.errno = code || 20010;
}
}
class ValidatedError extends Error {
public errmsg: string;
public errno: number;
constructor(msg: string, code: number) {
super(msg);
this.errmsg = msg || 'validated_error_msg';
this.errno = code || 20010;
}
}
// FETCH_ERROR
const errorHandle = (e: Error) => {
// do something
console.log(e, 'error');
if(e instanceof ValidatedError || e instanceof DbError) {
// do sth
return e;
}
return {
code: 101,
errmsg: 'unKnown'
};
}
const errorDecorator =
(handle: (e: Error) => void = errorHandle) =>
(fn: (...args: any[]) => Promise<{}>) =>
async(...args: any[]) => {
try {
console.log(...args, 'args');
return [null, await fn(...args)];
} catch(e) {
return [handle(e)];
}
}
const fetchFail = () => {
return Promise.reject('请求失败')
}
const asyncErrorWrapper = (errorHandler: (e: Error) => void = errorHandle) => (target: Function) => {
const props = Object.getOwnPropertyNames(target.prototype);
props.forEach((prop) => {
var value = target.prototype[prop];
if(Object.prototype.toString.call(value) === '[object AsyncFunction]'){
target.prototype[prop] = async (...args: any[]) => {
try{
return await value.apply(this,args);
}catch(err){
return errorHandler(err);
}
}
}
});
}
const usualHandleTryCatch = errorDecorator(errorHandle);
async function main1 () {
const a = usualHandleTryCatch(fetchFail)
const [error, res] = await a(false);
if(error) {
// 因为 catch 已经做了拦截,甚至可以加入一些通用逻辑,这里甚至不用判断 if error
console.log(error, 'error');
return;
}
console.log(res, 'res');
}
@asyncErrorWrapper(errorHandle)
class Store {
async getList (){
return Promise.reject();
}
}
const store = new Store();
async function main2() {
// console.log(store.getList, 'st');
const a = await store.getList();
console.log(a, 'aa');
// if(error) {
// // 因为 catch 已经做了拦截,甚至可以加入一些通用逻辑,这里甚至不用判断 if error
// console.log(error, 'error');
// return;
// }
// console.log(res, 'res');
}
main2()