/
reportingErrors.ts
536 lines (462 loc) · 16.1 KB
/
reportingErrors.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
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
import path from 'path'
import { toOutput$ } from '@pnpm/default-reporter'
import PnpmError from '@pnpm/error'
import logger, {
createStreamParser,
} from '@pnpm/logger'
import { map, take } from 'rxjs/operators'
import chalk from 'chalk'
import loadJsonFile from 'load-json-file'
import normalizeNewline from 'normalize-newline'
import StackTracey from 'stacktracey'
const formatErrorCode = (code: string) => chalk.bgRed.black(`\u2009${code}\u2009`)
const formatError = (code: string, message: string) => {
return `${formatErrorCode(code)} ${chalk.red(message)}`
}
const ERROR_PAD = ''
test('prints generic error', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new Error('some error')
logger.error(err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERROR', 'some error')}
${ERROR_PAD}${(new StackTracey(err.stack).asTable() as string).split('\n').join(`\n${ERROR_PAD}`)}`)
},
})
})
test('prints generic error when recursive install fails', (done) => {
const output$ = toOutput$({
context: { argv: ['recursive'] },
streamParser: createStreamParser(),
})
const err = new Error('some error')
err['prefix'] = '/home/src/'
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`/home/src/:
${formatError('ERROR', 'some error')}
${ERROR_PAD}
${ERROR_PAD}${(new StackTracey(err.stack).asTable() as string).split('\n').join(`\n${ERROR_PAD}`)}`)
},
})
})
test('prints no matching version error when many dist-tags exist', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_NO_MATCHING_VERSION', 'No matching version found for pnpm@1000.0.0')}
${ERROR_PAD}
${ERROR_PAD}The latest release of pnpm is "2.4.0".
${ERROR_PAD}
${ERROR_PAD}Other releases are:
${ERROR_PAD} * stable: 2.2.2
${ERROR_PAD} * next: 2.4.0
${ERROR_PAD} * latest-1: 1.43.1
${ERROR_PAD}
${ERROR_PAD}If you need the full list of all 281 published versions run "$ pnpm view pnpm versions".`)
},
})
const err = new PnpmError('NO_MATCHING_VERSION', 'No matching version found for pnpm@1000.0.0')
err['packageMeta'] = loadJsonFile.sync(path.join(__dirname, 'pnpm-meta.json'))
logger.error(err, err)
})
test('prints no matching version error when only the latest dist-tag exists', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_NO_MATCHING_VERSION', 'No matching version found for is-positive@1000.0.0')}
${ERROR_PAD}
${ERROR_PAD}The latest release of is-positive is "3.1.0".
${ERROR_PAD}
${ERROR_PAD}If you need the full list of all 4 published versions run "$ pnpm view is-positive versions".`)
},
})
const err = new PnpmError('NO_MATCHING_VERSION', 'No matching version found for is-positive@1000.0.0')
err['packageMeta'] = loadJsonFile.sync(path.join(__dirname, 'is-positive-meta.json'))
logger.error(err, err)
})
test('prints suggestions when an internet-connection related error happens', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_BAD_TARBALL_SIZE', 'Actual size (99) of tarball (https://foo) did not match the one specified in \'Content-Length\' header (100)')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing the dependencies of foo@1.0.0
${ERROR_PAD}
${ERROR_PAD}Seems like you have internet connection issues.
${ERROR_PAD}Try running the same command again.
${ERROR_PAD}If that doesn't help, try one of the following:
${ERROR_PAD}
${ERROR_PAD}- Set a bigger value for the \`fetch-retries\` config.
${ERROR_PAD} To check the current value of \`fetch-retries\`, run \`pnpm get fetch-retries\`.
${ERROR_PAD} To set a new value, run \`pnpm set fetch-retries <number>\`.
${ERROR_PAD}
${ERROR_PAD}- Set \`network-concurrency\` to 1.
${ERROR_PAD} This change will slow down installation times, so it is recommended to
${ERROR_PAD} delete the config once the internet connection is good again: \`pnpm config delete network-concurrency\`
${ERROR_PAD}
${ERROR_PAD}NOTE: You may also override configs via flags.
${ERROR_PAD}For instance, \`pnpm install --fetch-retries 5 --network-concurrency 1\``)
},
})
const err = new PnpmError('BAD_TARBALL_SIZE', 'Actual size (99) of tarball (https://foo) did not match the one specified in \'Content-Length\' header (100)')
err.pkgsStack = [
{
id: 'registry.npmjs.org/foo/1.0.0',
name: 'foo',
version: '1.0.0',
},
]
err['expectedSize'] = 100
err['receivedSize'] = 99
logger.error(err, err)
})
test('prints test error', (done) => {
const output$ = toOutput$({
context: { argv: ['run', 'test'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ELIFECYCLE', 'Test failed. See above for more details.')}`)
},
})
const err = new Error('Tests failed')
err['stage'] = 'test'
err['code'] = 'ELIFECYCLE'
logger.error(err, err)
})
test('prints command error with exit code', (done) => {
const output$ = toOutput$({
context: { argv: ['run', 'lint'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ELIFECYCLE', 'Command failed with exit code 100.')}`)
},
})
const err = new Error('Command failed')
err['errno'] = 100
err['stage'] = 'lint'
err['code'] = 'ELIFECYCLE'
logger.error(err, err)
})
test('prints command error without exit code', (done) => {
const output$ = toOutput$({
context: { argv: ['run', 'lint'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ELIFECYCLE', 'Command failed.')}`)
},
})
const err = new Error('Command failed')
err['stage'] = 'lint'
err['code'] = 'ELIFECYCLE'
logger.error(err, err)
})
test('prints unsupported pnpm version error', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_UNSUPPORTED_ENGINE', 'Unsupported environment (bad pnpm and/or Node.js version)')}
${ERROR_PAD}
${ERROR_PAD}Your pnpm version is incompatible with "/home/zoltan/project".
${ERROR_PAD}
${ERROR_PAD}Expected version: 2
${ERROR_PAD}Got: 3.0.0
${ERROR_PAD}
${ERROR_PAD}This is happening because the package's manifest has an engines.pnpm field specified.
${ERROR_PAD}To fix this issue, install the required pnpm version globally.
${ERROR_PAD}
${ERROR_PAD}To install the latest version of pnpm, run "pnpm i -g pnpm".
${ERROR_PAD}To check your pnpm version, run "pnpm -v".`)
},
})
const err = new PnpmError('UNSUPPORTED_ENGINE', 'Unsupported pnpm version')
err['packageId'] = '/home/zoltan/project'
err['wanted'] = { pnpm: '2' }
err['current'] = { pnpm: '3.0.0', node: '10.0.0' }
logger.error(err, err)
})
test('prints unsupported Node version error', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_UNSUPPORTED_ENGINE', 'Unsupported environment (bad pnpm and/or Node.js version)')}
${ERROR_PAD}
${ERROR_PAD}Your Node version is incompatible with "/home/zoltan/project".
${ERROR_PAD}
${ERROR_PAD}Expected version: >=12
${ERROR_PAD}Got: 10.0.0
${ERROR_PAD}
${ERROR_PAD}This is happening because the package's manifest has an engines.node field specified.
${ERROR_PAD}To fix this issue, install the required Node version.`)
},
})
const err = new PnpmError('UNSUPPORTED_ENGINE', 'Unsupported pnpm version')
err['packageId'] = '/home/zoltan/project'
err['wanted'] = { node: '>=12' }
err['current'] = { pnpm: '3.0.0', node: '10.0.0' }
logger.error(err, err)
})
test('prints unsupported pnpm and Node versions error', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_UNSUPPORTED_ENGINE', 'Unsupported environment (bad pnpm and/or Node.js version)')}
${ERROR_PAD}
${ERROR_PAD}Your pnpm version is incompatible with "/home/zoltan/project".
${ERROR_PAD}
${ERROR_PAD}Expected version: 2
${ERROR_PAD}Got: 3.0.0
${ERROR_PAD}
${ERROR_PAD}This is happening because the package's manifest has an engines.pnpm field specified.
${ERROR_PAD}To fix this issue, install the required pnpm version globally.
${ERROR_PAD}
${ERROR_PAD}To install the latest version of pnpm, run "pnpm i -g pnpm".
${ERROR_PAD}To check your pnpm version, run "pnpm -v".` + '\n\n' + `\
${ERROR_PAD}Your Node version is incompatible with "/home/zoltan/project".
${ERROR_PAD}
${ERROR_PAD}Expected version: >=12
${ERROR_PAD}Got: 10.0.0
${ERROR_PAD}
${ERROR_PAD}This is happening because the package's manifest has an engines.node field specified.
${ERROR_PAD}To fix this issue, install the required Node version.`)
},
})
const err = new PnpmError('UNSUPPORTED_ENGINE', 'Unsupported pnpm version')
err['packageId'] = '/home/zoltan/project'
err['wanted'] = { pnpm: '2', node: '>=12' }
err['current'] = { pnpm: '3.0.0', node: '10.0.0' }
logger.error(err, err)
})
test('prints error even if the error object not passed in through the message object', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new PnpmError('SOME_ERROR', 'some error')
logger.error(err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(formatError('ERR_PNPM_SOME_ERROR', 'some error'))
},
})
})
test('prints error without packages stacktrace when pkgsStack is empty', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new PnpmError('SOME_ERROR', 'some error')
err.pkgsStack = []
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(formatError('ERR_PNPM_SOME_ERROR', 'some error'))
},
})
})
test('prints error with packages stacktrace - depth 1 and hint', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new PnpmError('SOME_ERROR', 'some error', { hint: 'hint' })
err.pkgsStack = [
{
id: 'registry.npmjs.org/foo/1.0.0',
name: 'foo',
version: '1.0.0',
},
]
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_SOME_ERROR', 'some error')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing the dependencies of foo@1.0.0
${ERROR_PAD}hint`)
},
})
})
test('prints error with packages stacktrace - depth 2', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new PnpmError('SOME_ERROR', 'some error')
err.pkgsStack = [
{
id: 'registry.npmjs.org/foo/1.0.0',
name: 'foo',
version: '1.0.0',
},
{
id: 'registry.npmjs.org/bar/1.0.0',
name: 'bar',
version: '1.0.0',
},
]
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_SOME_ERROR', 'some error')}
${ERROR_PAD}
${ERROR_PAD}This error happened while installing the dependencies of foo@1.0.0
${ERROR_PAD} at bar@1.0.0`)
},
})
})
test('prints error and hint', (done) => {
const output$ = toOutput$({
context: { argv: ['install'] },
streamParser: createStreamParser(),
})
const err = new PnpmError('SOME_ERROR', 'some error', { hint: 'some hint' })
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(formatErrorCode('ERR_PNPM_SOME_ERROR') + ' ' + `${chalk.red('some error')}
some hint`)
},
})
})
test('prints authorization error with auth settings', (done) => {
const rawConfig = {
'//foo.bar:_auth': '9876543219',
'//foo.bar:_authToken': '9876543219',
'//foo.bar:_password': '9876543219',
'//foo.bar:username': 'kiss.reka',
'@foo:registry': 'https://foo.bar',
_auth: '0123456789',
_authToken: '0123456789',
_password: '0123456789',
'always-auth': false,
username: 'nagy.gabor',
}
const output$ = toOutput$({
context: { argv: ['install'], config: { rawConfig } as any }, // eslint-disable-line
streamParser: createStreamParser(),
})
const err = new PnpmError('FETCH_401', 'some error', { hint: 'some hint' })
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_FETCH_401', 'some error')}
${ERROR_PAD}
${ERROR_PAD}some hint
${ERROR_PAD}
${ERROR_PAD}These authorization settings were found:
${ERROR_PAD}//foo.bar:_auth=9876[hidden]
${ERROR_PAD}//foo.bar:_authToken=9876[hidden]
${ERROR_PAD}//foo.bar:_password=[hidden]
${ERROR_PAD}//foo.bar:username=kiss.reka
${ERROR_PAD}@foo:registry=https://foo.bar
${ERROR_PAD}_auth=0123[hidden]
${ERROR_PAD}_authToken=0123[hidden]
${ERROR_PAD}_password=[hidden]
${ERROR_PAD}always-auth=false
${ERROR_PAD}username=nagy.gabor`)
},
})
})
test('prints authorization error without auth settings, where there are none', (done) => {
const output$ = toOutput$({
context: { argv: ['install'], config: { rawConfig: {} } as any }, // eslint-disable-line
streamParser: createStreamParser(),
})
const err = new PnpmError('FETCH_401', 'some error', { hint: 'some hint' })
logger.error(err, err)
expect.assertions(1)
output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(`${formatError('ERR_PNPM_FETCH_401', 'some error')}
${ERROR_PAD}
${ERROR_PAD}some hint
${ERROR_PAD}
${ERROR_PAD}No authorization settings were found in the configs.
${ERROR_PAD}Try to log in to the registry by running "pnpm login"
${ERROR_PAD}or add the auth tokens manually to the ~/.npmrc file.`)
},
})
})