-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathderror.ts
More file actions
130 lines (110 loc) · 3.17 KB
/
derror.ts
File metadata and controls
130 lines (110 loc) · 3.17 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const { MoleculerError } = require('moleculer').Errors
interface IErrMapParams {
msg: string,
code: number,
external: boolean
}
interface IErrMap {
[propName: string]: IErrMapParams
}
let errMap: IErrMap = {}
export function SetErrorMap(newErrMap: IErrMap) {
errMap = newErrMap
}
//PERF: Can optimize stack by knowing call depth
export class DError {
private stack: string = ''
private message: string = ''
private name: string = 'DError'
constructor(prevError: any, private msg: string, private code: any | number = 500, private type: string = '',
private data: any = {}, private external: boolean = false) {
//In case someone sends data as null
if (!data)
data = {}
//If second parameter is type and third is data, use the error lookup table
if (typeof (code) === "object") {
type = msg
data = code
if (type in errMap) {
msg = errMap[type].msg
code = errMap[type].code
external = errMap[type].external
this.msg = msg;
this.code = code;
this.type = type;
this.data = data;
this.external = external;
}
else {
code = 500
}
}
this.stack = '\n' + (this.stack || Error().stack.split('\n', 3)[2])
this.msg = msg
this.external = external
if (!prevError) {
this.data.errStack = [{
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: { ...this.data } //To avoid circular references
}]
return
}
//Cross network errors will be DError but will not pass instanceof, so check name
if (!(prevError instanceof DError) && prevError.name as string !== 'DError') {
this.data.errStack = [{
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: { ...this.data }
}, {}]
if (prevError instanceof MoleculerError)
this.data.errStack[1] = {
msg: prevError.message,
code: prevError.message,
type: prevError.type,
data: prevError.data
}
else if (prevError instanceof Error)
this.data.errStack[1] = {
msg: prevError.message,
code: (prevError as any).code,
type: (prevError as any).type,
data: (prevError as any).data
}
else if (typeof prevError === 'string') {
this.data.errStack[1] = {
msg: prevError,
code: 500,
type: '',
external: false,
data: {}
}
}
if (prevError.stack)
this.stack += '\n' + prevError.stack
return
}
this.stack += prevError.stack
prevError.data.errStack.unshift({
msg: this.msg,
code: this.code,
type: this.type,
external: this.external,
data: { ...this.data }
})
this.data.errStack = prevError.data.errStack
//Older external errors take precedence to be shown to the user
if (prevError.external) {
this.message = prevError.message
this.msg = prevError.msg
this.code = prevError.code
this.type = prevError.type
this.data = prevError.data
this.external = true
}
}
}