Skip to content

Commit 5b38902

Browse files
committed
Handle unhandledRejections, help with cache eacces
Suggested by @godmar in https://npm.community/t/npm-err-cb-never-called-permission-denied/9167/5 Incidentally, this turned up that we're catching uncaughtExceptions in the main npm functions, but not unhandledRejections! Tracing this through, it seems like node-fetch-npm's use of cacache is particularly brittle. Any throw that comes from cacache is not caught properly, since node-fetch-npm is all streams and callbacks. The naive approach (just adding a catch and failing the callback) doesn't work, because then make-fetch-happen and npm-registry-fetch interpret the failure as an invalid response, when actually it was a local cache error. So, a bit more love and polish is definitely still needed in the guts of npm's fetching and caching code paths. In the meantime, though, handling any unhandledRejection at the top level prevents at least the worst and most useless type of error message. PR-URL: #227 Credit: @isaacs Close: #227 Reviewed-by: @isaacs
1 parent 99edd49 commit 5b38902

File tree

4 files changed

+80
-15
lines changed

4 files changed

+80
-15
lines changed

bin/npm-cli.js

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
log.info('using', 'node@%s', process.version)
6363

6464
process.on('uncaughtException', errorHandler)
65+
process.on('unhandledRejection', errorHandler)
6566

6667
if (conf.usage && npm.command !== 'help') {
6768
npm.argv.unshift(npm.command)

lib/utils/error-handler.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,12 @@ function errorHandler (er) {
187187
log.verbose('npm ', 'v' + npm.version)
188188

189189
;[
190+
'code',
191+
'syscall',
190192
'file',
191193
'path',
192-
'code',
193-
'errno',
194-
'syscall'
194+
'dest',
195+
'errno'
195196
].forEach(function (k) {
196197
var v = er[k]
197198
if (v) log.error(k, v)

lib/utils/error-message.js

+37-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var npm = require('../npm.js')
33
var util = require('util')
44
var nameValidator = require('validate-npm-package-name')
5+
var npmlog = require('npmlog')
56

67
module.exports = errorMessage
78

@@ -33,18 +34,42 @@ function errorMessage (er) {
3334

3435
case 'EACCES':
3536
case 'EPERM':
36-
short.push(['', er])
37-
detail.push([
38-
'',
39-
[
40-
'\nThe operation was rejected by your operating system.',
41-
(process.platform === 'win32'
42-
? 'It\'s possible that the file was already in use (by a text editor or antivirus),\nor that you lack permissions to access it.'
43-
: 'It is likely you do not have the permissions to access this file as the current user'),
44-
'\nIf you believe this might be a permissions issue, please double-check the',
45-
'permissions of the file and its containing directories, or try running',
46-
'the command again as root/Administrator (though this is not recommended).'
47-
].join('\n')])
37+
const isCachePath = typeof er.path === 'string' &&
38+
er.path.startsWith(npm.config.get('cache'))
39+
const isCacheDest = typeof er.dest === 'string' &&
40+
er.dest.startsWith(npm.config.get('cache'))
41+
42+
const isWindows = process.platform === 'win32'
43+
44+
if (!isWindows && (isCachePath || isCacheDest)) {
45+
// user probably doesn't need this, but still add it to the debug log
46+
npmlog.verbose(er.stack)
47+
short.push([
48+
'',
49+
[
50+
'',
51+
'Your cache folder contains root-owned files, due to a bug in',
52+
'previous versions of npm which has since been addressed.',
53+
'',
54+
'To permanently fix this problem, please run:',
55+
` sudo chown -R ${process.getuid()}:${process.getgid()} ${JSON.stringify(npm.config.get('cache'))}`
56+
].join('\n')
57+
])
58+
} else {
59+
short.push(['', er])
60+
detail.push([
61+
'',
62+
[
63+
'\nThe operation was rejected by your operating system.',
64+
(process.platform === 'win32'
65+
? 'It\'s possible that the file was already in use (by a text editor or antivirus),\n' +
66+
'or that you lack permissions to access it.'
67+
: 'It is likely you do not have the permissions to access this file as the current user'),
68+
'\nIf you believe this might be a permissions issue, please double-check the',
69+
'permissions of the file and its containing directories, or try running',
70+
'the command again as root/Administrator.'
71+
].join('\n')])
72+
}
4873
break
4974

5075
case 'ELIFECYCLE':
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const npm = require('../../lib/npm.js')
2+
const t = require('tap')
3+
4+
if (process.platform === 'win32') {
5+
t.plan(0, 'this is a unix-only thing')
6+
process.exit(0)
7+
}
8+
9+
const errorMessage = require('../../lib/utils/error-message.js')
10+
11+
const common = require('../common-tap.js')
12+
13+
t.plan(1)
14+
15+
npm.load({ cache: common.cache }, () => {
16+
npm.config.set('cache', common.cache)
17+
const er = new Error('access is e, i am afraid')
18+
er.code = 'EACCES'
19+
er.errno = -13
20+
er.path = common.cache + '/src'
21+
er.dest = common.cache + '/to'
22+
23+
t.match(errorMessage(er), {
24+
summary: [
25+
[
26+
'',
27+
new RegExp('\n' +
28+
'Your cache folder contains root-owned files, due to a bug in\n' +
29+
'previous versions of npm which has since been addressed.\n' +
30+
'\n' +
31+
'To permanently fix this problem, please run:\n' +
32+
' sudo chown -R [0-9]+:[0-9]+ ".*npm_cache_cache-eacces-error-message"'
33+
)
34+
]
35+
],
36+
detail: []
37+
}, 'get the helpful error message')
38+
})

0 commit comments

Comments
 (0)