-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
gerror-mixin.ts
78 lines (62 loc) · 1.87 KB
/
gerror-mixin.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
import { log } from 'wechaty-puppet'
import {
GError,
WrapAsync,
wrapAsyncError,
} from 'gerror'
import { wechatyCaptureException } from '../raven.js'
import type { WechatySkeleton } from '../wechaty/mod.js'
const gErrorMixin = <MixinBase extends typeof WechatySkeleton> (mixinBase: MixinBase) => {
log.verbose('WechatyGErrorMixin', 'gErrorMixin(%s)', mixinBase.name)
abstract class GErrorMixin extends mixinBase {
constructor (...args: any[]) {
super(...args)
}
/**
* Wrap promise in sync way (catch error by emitting it)
* 1. convert a async callback function to be sync function
* by catcing any errors and emit them to error event
* 2. wrap a Promise by catcing any errors and emit them to error event
*/
wrapAsync: WrapAsync = wrapAsyncError((e: any) => this.emit('error', e))
/**
* Wechaty internally can use `emit('error' whatever)` to emit any error
* But the external call can only emit GError.
* That's the reason why we need the below `emitError(e: any)
*/
emitError (e: any): void {
this.emit('error', e)
}
/**
* Convert any error to GError,
* and emit `error` event with GError
*/
override emit (event: any, ...args: any) {
if (event !== 'error') {
return super.emit(event, ...args)
}
/**
* Dealing with the `error` event
*/
const arg0 = args[0]
let gerror: GError
if (arg0 instanceof GError) {
gerror = arg0
} else {
gerror = GError.from(arg0)
}
wechatyCaptureException(gerror)
return super.emit('error', gerror)
}
}
return GErrorMixin
}
type GErrorMixin = ReturnType<typeof gErrorMixin>
type ProtectedPropertyGErrorMixin = never
export type {
GErrorMixin,
ProtectedPropertyGErrorMixin,
}
export {
gErrorMixin,
}