nodejs/deps/npm/lib/utils/error-handler.js

255 lines
6.0 KiB
JavaScript
Raw Normal View History

module.exports = errorHandler
module.exports.exit = exit
var cbCalled = false
var log = require('npmlog')
var npm = require('../npm.js')
var itWorked = false
var path = require('path')
var wroteLogFile = false
var exitCode = 0
var rollbacks = npm.rollbacks
var chain = require('slide').chain
var errorMessage = require('./error-message.js')
var replaceInfo = require('./replace-info.js')
var stopMetrics = require('./metrics.js').stop
const cacheFile = require('./cache-file.js')
var logFileName
function getLogFile () {
if (!logFileName) {
logFileName = path.resolve(npm.config.get('cache'), '_logs', (new Date()).toISOString().replace(/[.:]/g, '_') + '-debug.log')
}
return logFileName
}
var timings = {
version: npm.version,
command: process.argv.slice(2),
logfile: null
}
process.on('timing', function (name, value) {
if (timings[name]) { timings[name] += value } else { timings[name] = value }
})
process.on('exit', function (code) {
process.emit('timeEnd', 'npm')
log.disableProgress()
if (npm.config && npm.config.loaded && npm.config.get('timing')) {
try {
timings.logfile = getLogFile()
cacheFile.append('_timing.json', JSON.stringify(timings) + '\n')
} catch (_) {
// ignore
}
}
// kill any outstanding stats reporter if it hasn't finished yet
stopMetrics()
if (code) itWorked = false
if (itWorked) {
log.info('ok')
} else {
if (!cbCalled) {
log.error('', 'cb() never called!')
console.error('')
log.error('', 'This is an error with npm itself. Please report this error at:')
log.error('', ' <https://npm.community>')
writeLogFile()
}
2012-06-10 21:29:47 -07:00
2014-09-24 14:41:07 -07:00
if (code) {
log.verbose('code', code)
2014-09-24 14:41:07 -07:00
}
}
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
if (wroteLogFile) {
// just a line break
if (log.levels[log.level] <= log.levels.error) console.error('')
log.error(
'',
[
'A complete log of this run can be found in:',
' ' + getLogFile()
].join('\n')
)
wroteLogFile = false
}
2012-06-04 17:32:46 -07:00
var doExit = npm.config && npm.config.loaded && npm.config.get('_exit')
2012-06-04 17:32:46 -07:00
if (doExit) {
// actually exit.
if (exitCode === 0 && !itWorked) {
exitCode = 1
}
if (exitCode !== 0) process.exit(exitCode)
} else {
itWorked = false // ready for next exit
}
})
2012-06-04 17:32:46 -07:00
function exit (code, noLog) {
2013-10-24 09:21:59 -07:00
exitCode = exitCode || process.exitCode || code
2012-06-04 17:32:46 -07:00
var doExit = npm.config && npm.config.loaded ? npm.config.get('_exit') : true
log.verbose('exit', [code, doExit])
if (log.level === 'silent') noLog = true
2012-06-04 17:32:46 -07:00
2014-09-16 15:38:50 -07:00
if (rollbacks.length) {
chain(rollbacks.map(function (f) {
return function (cb) {
npm.commands.unbuild([f], true, cb)
}
}), function (er) {
if (er) {
log.error('error rolling back', er)
if (!code) {
errorHandler(er)
} else {
if (!noLog) writeLogFile()
reallyExit(er)
}
2014-09-16 15:38:50 -07:00
} else {
if (!noLog && code) writeLogFile()
reallyExit()
2014-09-16 15:38:50 -07:00
}
})
rollbacks.length = 0
} else if (code && !noLog) {
writeLogFile()
} else {
reallyExit()
2014-09-16 15:38:50 -07:00
}
2014-11-04 15:08:12 -08:00
function reallyExit (er) {
if (er && !code) code = typeof er.errno === 'number' ? er.errno : 1
2012-06-04 17:32:46 -07:00
itWorked = !code
// Exit directly -- nothing in the CLI should still be running in the
// background at this point, and this makes sure anything left dangling
// for whatever reason gets thrown away, instead of leaving the CLI open
//
// Commands that expect long-running actions should just delay `cb()`
process.stdout.write('', () => {
process.exit(code)
})
2012-06-04 17:32:46 -07:00
}
}
function errorHandler (er) {
log.disableProgress()
2014-07-31 09:05:30 -07:00
if (!npm.config || !npm.config.loaded) {
// logging won't work unless we pretend that it's ready
er = er || new Error('Exit prior to config file resolving.')
console.error(er.stack || er.message)
}
if (cbCalled) {
er = er || new Error('Callback called more than once.')
}
cbCalled = true
if (!er) return exit(0)
if (typeof er === 'string') {
log.error('', er)
2012-06-21 16:44:22 -07:00
return exit(1, true)
} else if (!(er instanceof Error)) {
log.error('weird error', er)
return exit(1, true)
}
var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
if (m && !er.code) {
er.code = m
}
2014-09-24 14:41:07 -07:00
;[
'type',
'stack',
'statusCode',
'pkgid'
].forEach(function (k) {
var v = er[k]
if (!v) return
v = replaceInfo(v)
log.verbose(k, v)
})
log.verbose('cwd', process.cwd())
var os = require('os')
var args = replaceInfo(process.argv)
log.verbose('', os.type() + ' ' + os.release())
log.verbose('argv', args.map(JSON.stringify).join(' '))
log.verbose('node', process.version)
log.verbose('npm ', 'v' + npm.version)
;[
deps: update npm to 6.10.3 BUGFIXES * [`27cccfbda`](https://github.com/npm/cli/commit/27cccfbdac8526cc807b07f416355949b1372a9b) [#223](https://github.com/npm/cli/pull/223) vulns → vulnerabilities in npm audit output ([@sapegin](https://github.com/sapegin)) * [`d5e865eb7`](https://github.com/npm/cli/commit/d5e865eb79329665a927cc2767b4395c03045dbb) [#222](https://github.com/npm/cli/pull/222) [#226](https://github.com/npm/cli/pull/226) install, doctor: don't crash if registry unset ([@dmitrydvorkin](https://github.com/dmitrydvorkin), [@isaacs](https://github.com/isaacs)) * [`5b3890226`](https://github.com/npm/cli/commit/5b389022652abeb0e1c278a152550eb95bc6c452) [#227](https://github.com/npm/cli/pull/227) [npm.community#9167](https://npm.community/t/npm-err-cb-never-called-permission-denied/9167/5) Handle unhandledRejections, tell user what to do when encountering an `EACCES` error in the cache. ([@isaacs](https://github.com/isaacs)) DEPENDENCIES * [`77516df6e`](https://github.com/npm/cli/commit/77516df6eac94a6d7acb5e9ca06feaa0868d779b) `licensee@7.0.3` ([@isaacs](https://github.com/isaacs)) * [`ceb993590`](https://github.com/npm/cli/commit/ceb993590e4e376a9a78264ce7bb4327fbbb37fe) `query-string@6.8.2` ([@isaacs](https://github.com/isaacs)) * [`4050b9189`](https://github.com/npm/cli/commit/4050b91898c60e9b22998cf82b70b9b822de592a) `hosted-git-info@2.8.2` * [#46](https://github.com/npm/hosted-git-info/issues/46) [#43](https://github.com/npm/hosted-git-info/issues/43) [#47](https://github.com/npm/hosted-git-info/pull/47) [#44](https://github.com/npm/hosted-git-info/pull/44) Add support for GitLab subgroups ([@mterrel](https://github.com/mterrel), [@isaacs](https://github.com/isaacs), [@ybiquitous](https://github.com/ybiquitous)) * [`3b1d629`](https://github.com/npm/hosted-git-info/commit/3b1d629) [#48](https://github.com/npm/hosted-git-info/issues/48) fix http protocol using sshurl by default ([@fengmk2](https://github.com/fengmk2)) * [`5d4a8d7`](https://github.com/npm/hosted-git-info/commit/5d4a8d7) ignore noCommittish on tarball url generation ([@isaacs](https://github.com/isaacs)) * [`1692435`](https://github.com/npm/hosted-git-info/commit/1692435) use gist tarball url that works for anonymous gists ([@isaacs](https://github.com/isaacs)) * [`d5cf830`](https://github.com/npm/hosted-git-info/commit/d5cf8309be7af884032616c63ea302ce49dd321c) Do not allow invalid gist urls ([@isaacs](https://github.com/isaacs)) * [`e518222`](https://github.com/npm/hosted-git-info/commit/e5182224351183ce619dd5ef00019ae700ed37b7) Use LRU cache to prevent unbounded memory consumption ([@iarna](https://github.com/iarna)) PR-URL: https://github.com/nodejs/node/pull/29023 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-08-06 09:53:43 -07:00
'code',
'syscall',
'file',
'path',
deps: update npm to 6.10.3 BUGFIXES * [`27cccfbda`](https://github.com/npm/cli/commit/27cccfbdac8526cc807b07f416355949b1372a9b) [#223](https://github.com/npm/cli/pull/223) vulns → vulnerabilities in npm audit output ([@sapegin](https://github.com/sapegin)) * [`d5e865eb7`](https://github.com/npm/cli/commit/d5e865eb79329665a927cc2767b4395c03045dbb) [#222](https://github.com/npm/cli/pull/222) [#226](https://github.com/npm/cli/pull/226) install, doctor: don't crash if registry unset ([@dmitrydvorkin](https://github.com/dmitrydvorkin), [@isaacs](https://github.com/isaacs)) * [`5b3890226`](https://github.com/npm/cli/commit/5b389022652abeb0e1c278a152550eb95bc6c452) [#227](https://github.com/npm/cli/pull/227) [npm.community#9167](https://npm.community/t/npm-err-cb-never-called-permission-denied/9167/5) Handle unhandledRejections, tell user what to do when encountering an `EACCES` error in the cache. ([@isaacs](https://github.com/isaacs)) DEPENDENCIES * [`77516df6e`](https://github.com/npm/cli/commit/77516df6eac94a6d7acb5e9ca06feaa0868d779b) `licensee@7.0.3` ([@isaacs](https://github.com/isaacs)) * [`ceb993590`](https://github.com/npm/cli/commit/ceb993590e4e376a9a78264ce7bb4327fbbb37fe) `query-string@6.8.2` ([@isaacs](https://github.com/isaacs)) * [`4050b9189`](https://github.com/npm/cli/commit/4050b91898c60e9b22998cf82b70b9b822de592a) `hosted-git-info@2.8.2` * [#46](https://github.com/npm/hosted-git-info/issues/46) [#43](https://github.com/npm/hosted-git-info/issues/43) [#47](https://github.com/npm/hosted-git-info/pull/47) [#44](https://github.com/npm/hosted-git-info/pull/44) Add support for GitLab subgroups ([@mterrel](https://github.com/mterrel), [@isaacs](https://github.com/isaacs), [@ybiquitous](https://github.com/ybiquitous)) * [`3b1d629`](https://github.com/npm/hosted-git-info/commit/3b1d629) [#48](https://github.com/npm/hosted-git-info/issues/48) fix http protocol using sshurl by default ([@fengmk2](https://github.com/fengmk2)) * [`5d4a8d7`](https://github.com/npm/hosted-git-info/commit/5d4a8d7) ignore noCommittish on tarball url generation ([@isaacs](https://github.com/isaacs)) * [`1692435`](https://github.com/npm/hosted-git-info/commit/1692435) use gist tarball url that works for anonymous gists ([@isaacs](https://github.com/isaacs)) * [`d5cf830`](https://github.com/npm/hosted-git-info/commit/d5cf8309be7af884032616c63ea302ce49dd321c) Do not allow invalid gist urls ([@isaacs](https://github.com/isaacs)) * [`e518222`](https://github.com/npm/hosted-git-info/commit/e5182224351183ce619dd5ef00019ae700ed37b7) Use LRU cache to prevent unbounded memory consumption ([@iarna](https://github.com/iarna)) PR-URL: https://github.com/nodejs/node/pull/29023 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-08-06 09:53:43 -07:00
'dest',
'errno'
].forEach(function (k) {
var v = er[k]
if (v) log.error(k, v)
})
2014-09-24 14:41:07 -07:00
var msg = errorMessage(er)
msg.summary.concat(msg.detail).forEach(function (errline) {
log.error.apply(log, errline)
})
if (npm.config && npm.config.get('json')) {
var error = {
error: {
code: er.code,
summary: messageText(msg.summary),
detail: messageText(msg.detail)
}
}
console.log(JSON.stringify(error, null, 2))
}
exit(typeof er.errno === 'number' ? er.errno : 1)
}
function messageText (msg) {
return msg.map(function (line) {
return line.slice(1).join(' ')
}).join('\n')
}
function writeLogFile () {
if (wroteLogFile) return
var os = require('os')
2012-06-10 21:29:47 -07:00
try {
var logOutput = ''
log.record.forEach(function (m) {
var pref = [m.id, m.level]
if (m.prefix) pref.push(m.prefix)
pref = pref.join(' ')
m.message.trim().split(/\r?\n/).map(function (line) {
return (pref + ' ' + line).trim()
}).forEach(function (line) {
logOutput += line + os.EOL
})
2012-06-10 21:29:47 -07:00
})
cacheFile.write(getLogFile(), logOutput)
// truncate once it's been written.
log.record.length = 0
wroteLogFile = true
} catch (ex) {
}
}