2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
module.exports = errorHandler
|
2017-05-09 14:46:02 -07:00
|
|
|
module.exports.exit = exit
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
var cbCalled = false
|
2015-10-09 23:13:57 -07:00
|
|
|
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
|
2016-01-28 18:11:35 -08:00
|
|
|
var errorMessage = require('./error-message.js')
|
2020-07-07 13:41:27 -05:00
|
|
|
var replaceInfo = require('./replace-info.js')
|
2017-01-12 16:58:10 -08:00
|
|
|
var stopMetrics = require('./metrics.js').stop
|
2019-07-24 23:00:03 -07:00
|
|
|
|
|
|
|
const cacheFile = require('./cache-file.js')
|
2017-02-14 15:12:22 -08:00
|
|
|
|
|
|
|
var logFileName
|
|
|
|
function getLogFile () {
|
|
|
|
if (!logFileName) {
|
|
|
|
logFileName = path.resolve(npm.config.get('cache'), '_logs', (new Date()).toISOString().replace(/[.:]/g, '_') + '-debug.log')
|
|
|
|
}
|
|
|
|
return logFileName
|
|
|
|
}
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2017-05-09 14:46:02 -07:00
|
|
|
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 }
|
|
|
|
})
|
|
|
|
|
2015-10-09 23:13:57 -07:00
|
|
|
process.on('exit', function (code) {
|
2017-05-09 14:46:02 -07:00
|
|
|
process.emit('timeEnd', 'npm')
|
2015-10-09 23:13:57 -07:00
|
|
|
log.disableProgress()
|
2020-02-26 17:11:08 -08:00
|
|
|
if (npm.config && npm.config.loaded && npm.config.get('timing')) {
|
2017-05-09 14:46:02 -07:00
|
|
|
try {
|
|
|
|
timings.logfile = getLogFile()
|
2019-07-24 23:00:03 -07:00
|
|
|
cacheFile.append('_timing.json', JSON.stringify(timings) + '\n')
|
2017-05-09 14:46:02 -07:00
|
|
|
} catch (_) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 16:58:10 -08:00
|
|
|
|
|
|
|
// kill any outstanding stats reporter if it hasn't finished yet
|
|
|
|
stopMetrics()
|
|
|
|
|
2011-11-21 09:48:45 -08:00
|
|
|
if (code) itWorked = false
|
2017-04-12 21:47:49 -07:00
|
|
|
if (itWorked) {
|
|
|
|
log.info('ok')
|
|
|
|
} else {
|
2011-11-21 09:48:45 -08:00
|
|
|
if (!cbCalled) {
|
2015-10-09 23:13:57 -07:00
|
|
|
log.error('', 'cb() never called!')
|
2017-04-12 21:47:49 -07:00
|
|
|
console.error('')
|
|
|
|
log.error('', 'This is an error with npm itself. Please report this error at:')
|
2018-07-18 13:55:52 -07:00
|
|
|
log.error('', ' <https://npm.community>')
|
2017-04-12 21:47:49 -07:00
|
|
|
writeLogFile()
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
2012-06-10 21:29:47 -07:00
|
|
|
|
2014-09-24 14:41:07 -07:00
|
|
|
if (code) {
|
2016-12-18 20:22:09 -08:00
|
|
|
log.verbose('code', code)
|
2014-09-24 14:41:07 -07:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
2020-02-26 17:11:08 -08:00
|
|
|
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
|
2017-05-09 14:46:02 -07:00
|
|
|
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
|
|
|
|
2020-02-26 17:11:08 -08: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
|
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2020-02-26 17:11:08 -08:00
|
|
|
var doExit = npm.config && npm.config.loaded ? npm.config.get('_exit') : true
|
2015-10-09 23:13:57 -07:00
|
|
|
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) {
|
2015-10-09 23:13:57 -07:00
|
|
|
log.error('error rolling back', er)
|
2017-04-12 21:47:49 -07:00
|
|
|
if (!code) {
|
|
|
|
errorHandler(er)
|
|
|
|
} else {
|
|
|
|
if (!noLog) writeLogFile()
|
|
|
|
reallyExit(er)
|
|
|
|
}
|
2014-09-16 15:38:50 -07:00
|
|
|
} else {
|
2017-04-12 21:47:49 -07:00
|
|
|
if (!noLog && code) writeLogFile()
|
|
|
|
reallyExit()
|
2014-09-16 15:38:50 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
rollbacks.length = 0
|
2016-05-27 14:07:59 -07:00
|
|
|
} else if (code && !noLog) {
|
2017-06-05 16:31:14 -07:00
|
|
|
writeLogFile()
|
2016-05-27 14:07:59 -07:00
|
|
|
} else {
|
2017-04-12 21:47:49 -07:00
|
|
|
reallyExit()
|
2014-09-16 15:38:50 -07:00
|
|
|
}
|
2014-11-04 15:08:12 -08:00
|
|
|
|
|
|
|
function reallyExit (er) {
|
2015-10-09 23:13:57 -07:00
|
|
|
if (er && !code) code = typeof er.errno === 'number' ? er.errno : 1
|
2012-06-04 17:32:46 -07:00
|
|
|
|
|
|
|
itWorked = !code
|
|
|
|
|
2017-05-28 21:04:08 -07:00
|
|
|
// 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()`
|
2017-07-14 10:52:48 -07:00
|
|
|
process.stdout.write('', () => {
|
|
|
|
process.exit(code)
|
|
|
|
})
|
2012-06-04 17:32:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-21 09:48:45 -08:00
|
|
|
function errorHandler (er) {
|
2015-10-09 23:13:57 -07:00
|
|
|
log.disableProgress()
|
2014-07-31 09:05:30 -07:00
|
|
|
if (!npm.config || !npm.config.loaded) {
|
2011-11-21 09:48:45 -08:00
|
|
|
// logging won't work unless we pretend that it's ready
|
2015-10-09 23:13:57 -07:00
|
|
|
er = er || new Error('Exit prior to config file resolving.')
|
2011-11-21 09:48:45 -08:00
|
|
|
console.error(er.stack || er.message)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cbCalled) {
|
2015-10-09 23:13:57 -07:00
|
|
|
er = er || new Error('Callback called more than once.')
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
cbCalled = true
|
|
|
|
if (!er) return exit(0)
|
2015-10-09 23:13:57 -07:00
|
|
|
if (typeof er === 'string') {
|
|
|
|
log.error('', er)
|
2012-06-21 16:44:22 -07:00
|
|
|
return exit(1, true)
|
|
|
|
} else if (!(er instanceof Error)) {
|
2015-10-09 23:13:57 -07:00
|
|
|
log.error('weird error', er)
|
2011-11-21 09:48:45 -08:00
|
|
|
return exit(1, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = er.code || er.message.match(/^(?:Error: )?(E[A-Z]+)/)
|
2015-10-09 23:13:57 -07:00
|
|
|
if (m && !er.code) {
|
|
|
|
er.code = m
|
|
|
|
}
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2015-10-09 23:13:57 -07:00
|
|
|
;[
|
|
|
|
'type',
|
|
|
|
'stack',
|
|
|
|
'statusCode',
|
|
|
|
'pkgid'
|
|
|
|
].forEach(function (k) {
|
|
|
|
var v = er[k]
|
|
|
|
if (!v) return
|
2020-07-07 13:41:27 -05:00
|
|
|
v = replaceInfo(v)
|
2015-10-09 23:13:57 -07:00
|
|
|
log.verbose(k, v)
|
|
|
|
})
|
|
|
|
|
|
|
|
log.verbose('cwd', process.cwd())
|
|
|
|
|
|
|
|
var os = require('os')
|
2020-07-07 13:41:27 -05:00
|
|
|
var args = replaceInfo(process.argv)
|
2017-04-12 21:47:49 -07:00
|
|
|
log.verbose('', os.type() + ' ' + os.release())
|
2020-07-07 13:41:27 -05:00
|
|
|
log.verbose('argv', args.map(JSON.stringify).join(' '))
|
2017-04-12 21:47:49 -07:00
|
|
|
log.verbose('node', process.version)
|
|
|
|
log.verbose('npm ', 'v' + npm.version)
|
2015-10-09 23:13:57 -07:00
|
|
|
|
|
|
|
;[
|
2019-08-06 09:53:43 -07:00
|
|
|
'code',
|
|
|
|
'syscall',
|
2015-10-09 23:13:57 -07:00
|
|
|
'file',
|
|
|
|
'path',
|
2019-08-06 09:53:43 -07:00
|
|
|
'dest',
|
|
|
|
'errno'
|
2015-10-09 23:13:57 -07:00
|
|
|
].forEach(function (k) {
|
|
|
|
var v = er[k]
|
|
|
|
if (v) log.error(k, v)
|
|
|
|
})
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2016-01-28 18:11:35 -08:00
|
|
|
var msg = errorMessage(er)
|
|
|
|
msg.summary.concat(msg.detail).forEach(function (errline) {
|
|
|
|
log.error.apply(log, errline)
|
|
|
|
})
|
2019-01-29 14:43:00 -08:00
|
|
|
if (npm.config && npm.config.get('json')) {
|
2017-05-09 14:46:02 -07:00
|
|
|
var error = {
|
|
|
|
error: {
|
|
|
|
code: er.code,
|
|
|
|
summary: messageText(msg.summary),
|
|
|
|
detail: messageText(msg.detail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log(JSON.stringify(error, null, 2))
|
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2015-10-09 23:13:57 -07:00
|
|
|
exit(typeof er.errno === 'number' ? er.errno : 1)
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
|
|
|
|
2017-05-09 14:46:02 -07:00
|
|
|
function messageText (msg) {
|
|
|
|
return msg.map(function (line) {
|
|
|
|
return line.slice(1).join(' ')
|
|
|
|
}).join('\n')
|
|
|
|
}
|
|
|
|
|
2017-04-12 21:47:49 -07:00
|
|
|
function writeLogFile () {
|
|
|
|
if (wroteLogFile) return
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2015-10-09 23:13:57 -07:00
|
|
|
var os = require('os')
|
2012-06-10 21:29:47 -07:00
|
|
|
|
2017-04-12 21:47:49 -07:00
|
|
|
try {
|
|
|
|
var logOutput = ''
|
2017-02-14 15:12:22 -08:00
|
|
|
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) {
|
2017-04-12 21:47:49 -07:00
|
|
|
logOutput += line + os.EOL
|
2017-02-14 15:12:22 -08:00
|
|
|
})
|
2012-06-10 21:29:47 -07:00
|
|
|
})
|
2019-07-24 23:00:03 -07:00
|
|
|
cacheFile.write(getLogFile(), logOutput)
|
2017-05-09 14:46:02 -07:00
|
|
|
|
|
|
|
// truncate once it's been written.
|
|
|
|
log.record.length = 0
|
|
|
|
wroteLogFile = true
|
2017-04-12 21:47:49 -07:00
|
|
|
} catch (ex) {
|
2018-04-20 18:26:37 -07:00
|
|
|
|
2017-04-12 21:47:49 -07:00
|
|
|
}
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|