/
error-handler.js
589 lines (483 loc) · 12.6 KB
/
error-handler.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* eslint-disable no-extend-native */
/* eslint-disable no-global-assign */
const EventEmitter = require('events')
const requireInject = require('require-inject')
const t = require('tap')
// NOTE: Although these unit tests may look like the rest on the surface,
// they are in fact very special due to the amount of things hooking directly
// to global process and variables defined in the module scope. That makes
// for tests that are very interdependent and their order are important.
// generic error to be used in tests
const err = Object.assign(new Error('ERROR'), { code: 'ERROR' })
err.stack = 'Error: ERROR'
const redactCwd = (path) => {
const normalizePath = p => p
.replace(/\\+/g, '/')
.replace(/\r\n/g, '\n')
return normalizePath(path)
.replace(new RegExp(normalizePath(process.cwd()), 'g'), '{CWD}')
}
t.cleanSnapshot = (str) => redactCwd(str)
// internal modules mocks
const cacheFile = {
append: () => null,
write: () => null,
}
const config = {
values: {
cache: 'cachefolder',
timing: true,
},
loaded: true,
updateNotification: null,
get (key) {
return this.values[key]
},
}
const npm = {
version: '1.0.0',
config,
shelloutCommands: ['exec', 'run-script'],
}
const npmlog = {
disableProgress: () => null,
log (level, ...args) {
this.record.push({
id: this.record.length,
level,
message: args.reduce((res, i) => `${res} ${i.message ? i.message : i}`, ''),
prefix: level !== 'verbose' ? 'foo' : '',
})
},
error (...args) {
this.log('error', ...args)
},
info (...args) {
this.log('info', ...args)
},
level: 'silly',
levels: {
silly: 0,
verbose: 1,
info: 2,
error: 3,
silent: 4,
},
notice (...args) {
this.log('notice', ...args)
},
record: [],
verbose (...args) {
this.log('verbose', ...args)
},
}
// overrides OS type/release for cross platform snapshots
const os = require('os')
os.type = () => 'Foo'
os.release = () => '1.0.0'
// bootstrap tap before cutting off process ref
t.test('ok', (t) => {
t.ok('ok')
t.end()
})
// cut off process from script so that it won't quit the test runner
// while trying to run through the myriad of cases
const _process = process
process = Object.assign(
new EventEmitter(),
{
argv: ['/node', ..._process.argv.slice(1)],
cwd: _process.cwd,
env: _process.env,
exit () {},
exitCode: 0,
version: 'v1.0.0',
stdout: { write (_, cb) {
cb()
} },
stderr: { write () {} },
}
)
// needs to put process back in its place
// in order for tap to exit properly
t.teardown(() => {
process = _process
})
const mocks = {
npmlog,
'../../../lib/npm.js': npm,
'../../../lib/utils/error-message.js': (err) => ({
...err,
summary: [['ERR', err.message]],
detail: [['ERR', err.message]],
}),
'../../../lib/utils/cache-file.js': cacheFile,
}
requireInject.installGlobally('../../../lib/utils/error-handler.js', mocks)
let errorHandler = require('../../../lib/utils/error-handler.js')
t.test('default exit code', (t) => {
t.plan(1)
// manually simulate timing handlers
process.emit('timing', 'foo', 1)
process.emit('timing', 'foo', 2)
// generates logfile name with mocked date
const _toISOString = Date.prototype.toISOString
Date.prototype.toISOString = () => 'expecteddate'
npmlog.level = 'silent'
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 1, 'should default to error code 1')
}
// skip console.error logs
const _error = console.error
console.error = () => null
process.emit('exit', 1)
t.teardown(() => {
npmlog.level = 'silly'
process.exit = _exit
console.error = _error
Date.prototype.toISOString = _toISOString
})
})
t.test('handles unknown error', (t) => {
t.plan(2)
cacheFile.write = (filename, content) => {
t.equal(
redactCwd(filename),
'{CWD}/cachefolder/_logs/expecteddate-debug.log',
'should use expected log filename'
)
t.matchSnapshot(
content,
'should have expected log contents for unknown error'
)
}
errorHandler(err)
t.teardown(() => {
cacheFile.write = () => null
})
t.end()
})
t.test('npm.config not ready', (t) => {
t.plan(1)
config.loaded = false
const _error = console.error
console.error = (msg) => {
t.match(
msg,
/Error: Exit prior to config file resolving./,
'should exit with config error msg'
)
}
errorHandler()
t.teardown(() => {
console.error = _error
config.loaded = true
})
})
t.test('fail to write logfile', (t) => {
t.plan(1)
cacheFile.write = () => {
throw err
}
t.teardown(() => {
cacheFile.write = () => null
})
t.doesNotThrow(
() => errorHandler(err),
'should not throw on cache write failure'
)
})
t.test('console.log output using --json', (t) => {
t.plan(1)
config.values.json = true
const _error = console.error
console.error = (jsonOutput) => {
t.deepEqual(
JSON.parse(jsonOutput),
{
error: {
code: 'EBADTHING', // should default error code to E[A-Z]+
summary: 'Error: EBADTHING Something happened',
detail: 'Error: EBADTHING Something happened',
},
},
'should output expected json output'
)
}
errorHandler(new Error('Error: EBADTHING Something happened'))
t.teardown(() => {
console.error = _error
delete config.values.json
})
})
t.test('throw a non-error obj', (t) => {
t.plan(3)
const weirdError = {
code: 'ESOMETHING',
message: 'foo bar',
}
const _logError = npmlog.error
npmlog.error = (title, err) => {
t.equal(title, 'weird error', 'should name it a weird error')
t.deepEqual(err, weirdError, 'should log given weird error')
}
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 1, 'should exit with code 1')
}
errorHandler(weirdError)
t.teardown(() => {
process.exit = _exit
npmlog.error = _logError
})
})
t.test('throw a string error', (t) => {
t.plan(3)
const error = 'foo bar'
const _logError = npmlog.error
npmlog.error = (title, err) => {
t.equal(title, '', 'should have an empty name ref')
t.deepEqual(err, 'foo bar', 'should log string error')
}
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 1, 'should exit with code 1')
}
errorHandler(error)
t.teardown(() => {
process.exit = _exit
npmlog.error = _logError
})
})
t.test('update notification', (t) => {
t.plan(2)
const updateMsg = 'you should update npm!'
npm.updateNotification = updateMsg
const _notice = npmlog.notice
npmlog.notice = (prefix, msg) => {
t.equal(prefix, '', 'should have no prefix')
t.equal(msg, updateMsg, 'should show update message')
}
errorHandler(err)
t.teardown(() => {
npmlog.notice = _notice
delete npm.updateNotification
})
})
t.test('on exit handler', (t) => {
t.plan(2)
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 1, 'should default to error code 1')
}
process.once('timeEnd', (msg) => {
t.equal(msg, 'npm', 'should trigger timeEnd for npm')
})
// skip console.error logs
const _error = console.error
console.error = () => null
process.emit('exit', 1)
t.teardown(() => {
console.error = _error
process.exit = _exit
})
})
t.test('it worked', (t) => {
t.plan(2)
config.values.timing = false
const _exit = process.exit
process.exit = (code) => {
process.exit = _exit
t.equal(code, 0, 'should exit with code 0')
const _info = npmlog.info
npmlog.info = (msg) => {
npmlog.info = _info
t.equal(msg, 'ok', 'should log ok if "it worked"')
}
process.emit('exit', 0)
}
t.teardown(() => {
process.exit = _exit
config.values.timing = true
})
errorHandler.exit(0)
})
t.test('uses code from errno', (t) => {
t.plan(1)
// RESET MODULE INTERNAL VARS AND GLOBAL REFS
requireInject.installGlobally.andClearCache('../../../lib/utils/error-handler.js', mocks)
errorHandler = require('../../../lib/utils/error-handler.js')
npmlog.level = 'silent'
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 127, 'should use set errno')
}
errorHandler(Object.assign(
new Error('Error with errno'),
{
errno: 127,
}
))
t.teardown(() => {
npmlog.level = 'silly'
process.exit = _exit
})
})
t.test('uses exitCode as code if using a number', (t) => {
t.plan(1)
// RESET MODULE INTERNAL VARS AND GLOBAL REFS
requireInject.installGlobally.andClearCache(
'../../../lib/utils/error-handler.js',
mocks
)
errorHandler = require('../../../lib/utils/error-handler.js')
npmlog.level = 'silent'
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 404, 'should use code if a number')
}
errorHandler(Object.assign(
new Error('Error with code type number'),
{
code: 404,
}
))
t.teardown(() => {
npmlog.level = 'silly'
process.exit = _exit
})
})
t.test('call errorHandler with no error', (t) => {
t.plan(1)
// RESET MODULE INTERNAL VARS AND GLOBAL REFS
requireInject.installGlobally.andClearCache(
'../../../lib/utils/error-handler.js',
mocks
)
errorHandler = require('../../../lib/utils/error-handler.js')
const _exit = process.exit
process.exit = (code) => {
t.equal(code, 0, 'should exit with code 0')
}
t.teardown(() => {
process.exit = _exit
})
errorHandler()
})
t.test('callback called twice', (t) => {
t.plan(2)
const _verbose = npmlog.verbose
npmlog.verbose = (key, value) => {
t.equal(key, 'stack', 'should log stack in verbose level')
t.match(
value,
/Error: Callback called more than once./,
'should have expected error msg'
)
npmlog.verbose = _verbose
}
errorHandler()
})
t.test('defaults to log error msg if stack is missing', (t) => {
t.plan(1)
const noStackErr = Object.assign(
new Error('Error with no stack'),
{
code: 'ENOSTACK',
errno: 127,
}
)
delete noStackErr.stack
npm.config.loaded = false
const _error = console.error
console.error = (msg) => {
console.error = _error
npm.config.loaded = true
t.equal(msg, 'Error with no stack', 'should use error msg')
}
errorHandler(noStackErr)
})
t.test('set it worked', (t) => {
t.plan(1)
// RESET MODULE INTERNAL VARS AND GLOBAL REFS
requireInject.installGlobally.andClearCache(
'../../../lib/utils/error-handler.js',
mocks
)
errorHandler = require('../../../lib/utils/error-handler.js')
const _exit = process.exit
process.exit = () => {
t.ok('ok')
}
t.teardown(() => {
process.exit = _exit
})
errorHandler.exit(0, true)
})
t.test('use exitCode when emitting exit event', (t) => {
t.plan(1)
npmlog.level = 'silent'
const _exit = process.exit
process.exit = (code) => {
process.exit = _exit
t.equal(code, 1, 'should exit with code 1')
}
t.teardown(() => {
process.exit = _exit
npmlog.level = 'silly'
})
process.emit('exit')
})
t.test('do no fancy handling for shellouts', t => {
const { exit } = process
const { command } = npm
const { log } = npmlog
const LOG_RECORD = []
t.teardown(() => {
npmlog.log = log
process.exit = exit
npm.command = command
})
npmlog.log = function (level, ...args) {
log.call(this, level, ...args)
LOG_RECORD.push(npmlog.record[npmlog.record.length - 1])
}
npm.command = 'exec'
let EXPECT_EXIT = 0
process.exit = code => {
t.equal(code, EXPECT_EXIT, 'got expected exit code')
EXPECT_EXIT = 0
}
t.beforeEach((cb) => {
LOG_RECORD.length = 0
cb()
})
const loudNoises = () => LOG_RECORD
.filter(({ level }) => ['warn', 'error'].includes(level))
t.test('shellout with a numeric error code', t => {
EXPECT_EXIT = 5
errorHandler(Object.assign(new Error(), { code: 5 }))
t.equal(EXPECT_EXIT, 0, 'called process.exit')
// should log no warnings or errors, verbose/silly is fine.
t.strictSame(loudNoises(), [], 'no noisy warnings')
t.end()
})
t.test('shellout without a numeric error code (something in npm)', t => {
EXPECT_EXIT = 1
errorHandler(Object.assign(new Error(), { code: 'banana stand' }))
t.equal(EXPECT_EXIT, 0, 'called process.exit')
// should log some warnings and errors, because something weird happened
t.strictNotSame(loudNoises(), [], 'bring the noise')
t.end()
})
t.test('shellout with code=0 (extra weird?)', t => {
EXPECT_EXIT = 1
errorHandler(Object.assign(new Error(), { code: 0 }))
t.equal(EXPECT_EXIT, 0, 'called process.exit')
// should log some warnings and errors, because something weird happened
t.strictNotSame(loudNoises(), [], 'bring the noise')
t.end()
})
t.end()
})