forked from CrabDude/trycatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trycatch.old.js
executable file
·249 lines (212 loc) · 5.62 KB
/
trycatch.old.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var domain = require('domain')
, path = require('path')
, hookit = require('hookit')
, originalFormatError = require('./formatError').formatError
, normalizeError = require('./formatError').normalizeError
, fileNameFilter = [path.dirname(__filename), require.resolve('hookit'), 'domain.js', 'Domain.emit']
, delimitter = '\n ----------------------------------------\n'
, hasGuardedWithoutLST
, options
options = {
'long-stack-traces': false
, 'colors': {
'node': 'white',
'node_modules': 'cyan',
'default': 'red'
}
, 'format': null
, 'filter': fileNameFilter
}
function trycatch(tryFn, catchFn) {
var parentDomain
, isLST
, d
if ('function' !== typeof tryFn || 'function' !== typeof catchFn) {
throw new Error('tryFn and catchFn must be functions')
}
parentDomain = domain.active
isLST = options['long-stack-traces']
d = domain.create()
if (isLST && parentDomain) {
d.parentStack = parentDomain.currentStack
}
d.on('error', _trycatchOnError)
runInDomain(d, tryFn, trycatchit)
return d
function _trycatchOnError(e) {
if (!e.caught) {
if (false === e.domainThrown || false === e.domain_thrown) {
// "error" emitted, passed directly to domain
e = normalizeError(e)
} else {
d.exit()
throw e
}
}
if (isLST) {
if (e.rethrown) {
if (e.parentStack) {
e.stack += delimitter + e.parentStack
}
} else if (d.currentStack) {
e.stack += delimitter + d.currentStack
}
}
e = formatError(e)
if (isLST && d.parentStack) {
e.parentStack = d.parentStack
}
d.exit()
handleException(e, parentDomain, catchFn)
}
}
// Generate a new callback
// Ensure it runs in the same domain
function wrap(callback, name) {
var isLST
, stack
, d
if ('function' !== typeof callback || callback && callback.name === '_trycatchOnError') return callback
isLST = options['long-stack-traces']
d = process.domain
if (isLST) {
stack = getStack(d)
}
// Inherit from callback for when properties are added
newCallback.__proto__ = callback
return newCallback
function newCallback() {
if (isLST && d) d.currentStack = stack
runInDomain(d, callback, trycatchitApply, [this, arguments])
}
}
function getStack(parentDomain) {
var stack
if (options['long-stack-traces']) {
stack = new Error().stack.substr(6)
if (parentDomain && parentDomain.currentStack) {
stack += delimitter + parentDomain.currentStack
}
}
return stack
}
function runInDomain(d, tryFn, trycatchitFn, arg) {
var hasDomain = d && !d._disposed
var enterExit = hasDomain && process.domain !== d
enterExit && d.enter()
var err = trycatchitFn(tryFn, arg)
enterExit && d.exit()
if (err) {
if (hasDomain) {
d.emit('error', normalizeError(err))
} else {
handleException(formatError(normalizeError(err)))
}
}
}
// To avoid V8 deopt
function trycatchitApply(tryFn, args) {
try {
tryFn.apply(args[0], args[1])
} catch(e) {
return e
}
}
// To avoid V8 deopt
function trycatchit1(tryFn, arg) {
try {
tryFn.call(null, arg)
} catch(e) {
return e
}
}
// To avoid V8 deopt
function trycatchit(tryFn) {
try {
tryFn()
} catch(e) {
return e
}
}
function handleException(err, parentDomain, catchFn) {
var caught
, secondError
if (!err.catchable) {
// Unexpected error, core in undefined state, requires restart
caught = process.emit('uncaughtException', err)
// Otherwise crash the process
if (!caught) {
throw err
}
return
}
if (!parentDomain) {
if ('function' === typeof catchFn) {
secondError = trycatchit1(catchFn, err)
if (secondError) {
handleException(formatError(normalizeError(secondError)));
}
return
}
caught = process.emit('uncaughtApplicationException', err)
if (!caught) {
caught = process.emit('uncaughtException', err)
}
if (!caught) {
throw err
}
return
}
runInDomain(parentDomain, catchFn, trycatchit1, err)
}
function formatError(err) {
return originalFormatError(err
, {
'long-stack-traces': options['long-stack-traces']
, lineFormatter: trycatch.format
, colors: options.colors
, filter: options.filter
})
}
/* Config Logic */
function configure(opts) {
if (null != opts['long-stack-traces']) {
opts['long-stack-traces'] = Boolean(opts['long-stack-traces'])
if (opts['long-stack-traces']) {
if (hasGuardedWithoutLST) {
console.warn('Turning long-stack-traces off can result in indeterminate behavior.')
}
// No longer necessary, but nice to have
Error.stackTraceLimit = Infinity
} else {
Error.stackTraceLimit = 10
}
options['long-stack-traces'] = opts['long-stack-traces']
}
if (null != opts.colors && 'object' === typeof opts.colors) {
if ('undefined' !== typeof opts.colors.node) {
options.colors.node = opts.colors.node
}
if ('undefined' !== typeof opts.colors.node_modules) {
options.colors.node_modules = opts.colors.node_modules
}
if ('undefined' !== typeof opts.colors.default) {
options.colors.default = opts.colors.default
}
}
if ('undefined' !== typeof opts.format) {
if ('function' === typeof opts.format) {
trycatch.format = options.format = opts.format
} else if (!Boolean(opts.format)) {
trycatch.format = null
}
}
if (Array.isArray(opts.filter)) {
options.filter = opts.filter
}
}
// Pass callback wrapping function to hookit
hookit(wrap)
trycatch.configure = configure
trycatch.format = null
module.exports = trycatch