2024-04-30 23:53:22 -07:00
|
|
|
const { log, output, META } = require('proc-log')
|
|
|
|
const errorMessage = require('../utils/error-message.js')
|
2024-04-07 14:36:14 -07:00
|
|
|
const { redactLog: replaceInfo } = require('@npmcli/redact')
|
2019-07-24 23:00:03 -07:00
|
|
|
|
2021-12-02 22:04:46 +00:00
|
|
|
let npm = null // set by the cli
|
|
|
|
let exitHandlerCalled = false
|
2022-03-31 22:43:17 +00:00
|
|
|
let showLogFileError = false
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2020-10-02 17:52:19 -04:00
|
|
|
process.on('exit', code => {
|
2022-12-06 22:18:33 -05:00
|
|
|
const hasLoadedNpm = npm?.config.loaded
|
2021-12-02 22:04:46 +00:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (!code) {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.info('ok')
|
2021-11-18 20:58:02 +00:00
|
|
|
} else {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.verbose('code', code)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-07-27 16:39:44 +00:00
|
|
|
|
|
|
|
if (!exitHandlerCalled) {
|
|
|
|
process.exitCode = code || 1
|
2021-12-02 22:04:46 +00:00
|
|
|
log.error('', 'Exit handler never called!')
|
|
|
|
log.error('', 'This is an error with npm itself. Please report this error at:')
|
|
|
|
log.error('', ' <https://github.com/npm/cli/issues>')
|
2024-04-30 23:53:22 -07:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('')
|
2022-03-31 22:43:17 +00:00
|
|
|
showLogFileError = true
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-12-02 22:04:46 +00:00
|
|
|
|
|
|
|
// npm must be loaded to know where the log file was written
|
2022-03-31 22:43:17 +00:00
|
|
|
if (hasLoadedNpm) {
|
2024-04-30 23:53:22 -07:00
|
|
|
npm.finish({ showLogFileError })
|
2022-03-31 22:43:17 +00:00
|
|
|
// This removes any listeners npm setup, mostly for tests to avoid max listener warnings
|
2021-12-02 22:04:46 +00:00
|
|
|
npm.unload()
|
|
|
|
}
|
|
|
|
|
2021-06-24 21:39:48 +00:00
|
|
|
// these are needed for the tests to have a clean slate in each test case
|
|
|
|
exitHandlerCalled = false
|
2022-03-31 22:43:17 +00:00
|
|
|
showLogFileError = false
|
2011-11-21 09:48:45 -08:00
|
|
|
})
|
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
const exitHandler = err => {
|
2021-12-02 22:04:46 +00:00
|
|
|
exitHandlerCalled = true
|
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
const hasLoadedNpm = npm?.config.loaded
|
2021-12-02 22:04:46 +00:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
if (!npm) {
|
2021-12-02 22:04:46 +00:00
|
|
|
err = err || new Error('Exit prior to setting npm in exit handler')
|
2024-04-30 23:53:22 -07:00
|
|
|
// Don't use proc-log here since npm was never set
|
2022-03-31 22:43:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-12-02 22:04:46 +00:00
|
|
|
console.error(err.stack || err.message)
|
|
|
|
return process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasLoadedNpm) {
|
2021-06-24 21:39:48 +00:00
|
|
|
err = err || new Error('Exit prior to config file resolving.')
|
2024-04-30 23:53:22 -07:00
|
|
|
// Don't use proc-log here since npm was never loaded
|
2022-03-31 22:43:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-06-24 21:39:48 +00:00
|
|
|
console.error(err.stack || err.message)
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
|
|
|
|
2021-07-15 20:09:18 +00:00
|
|
|
// only show the notification if it finished.
|
2021-06-03 20:17:35 +00:00
|
|
|
if (typeof npm.updateNotification === 'string') {
|
2024-04-30 23:53:22 -07:00
|
|
|
log.notice('', npm.updateNotification, { [META]: true, force: true })
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
|
|
|
|
2023-05-19 06:45:02 -07:00
|
|
|
let exitCode = process.exitCode || 0
|
|
|
|
let noLogMessage = exitCode !== 0
|
2022-12-06 22:18:33 -05:00
|
|
|
let jsonError
|
2021-07-15 20:09:18 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
exitCode = 1
|
|
|
|
// if we got a command that just shells out to something else, then it
|
|
|
|
// will presumably print its own errors and exit with a proper status
|
|
|
|
// code if there's a problem. If we got an error with a code=0, then...
|
|
|
|
// something else went wrong along the way, so maybe an npm problem?
|
2023-01-16 22:38:23 -05:00
|
|
|
const isShellout = npm.isShellout
|
2021-07-15 20:09:18 +00:00
|
|
|
const quietShellout = isShellout && typeof err.code === 'number' && err.code
|
|
|
|
if (quietShellout) {
|
|
|
|
exitCode = err.code
|
2021-12-02 22:04:46 +00:00
|
|
|
noLogMessage = true
|
2021-07-15 20:09:18 +00:00
|
|
|
} else if (typeof err === 'string') {
|
2021-12-09 21:20:18 +00:00
|
|
|
// XXX: we should stop throwing strings
|
2021-12-02 22:04:46 +00:00
|
|
|
log.error('', err)
|
|
|
|
noLogMessage = true
|
2021-07-15 20:09:18 +00:00
|
|
|
} else if (!(err instanceof Error)) {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.error('weird error', err)
|
|
|
|
noLogMessage = true
|
2021-07-15 20:09:18 +00:00
|
|
|
} else {
|
2024-04-30 23:53:22 -07:00
|
|
|
const os = require('node:os')
|
|
|
|
const fs = require('node:fs')
|
2021-07-15 20:09:18 +00:00
|
|
|
if (!err.code) {
|
|
|
|
const matchErrorCode = err.message.match(/^(?:Error: )?(E[A-Z]+)/)
|
|
|
|
err.code = matchErrorCode && matchErrorCode[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const k of ['type', 'stack', 'statusCode', 'pkgid']) {
|
|
|
|
const v = err[k]
|
2021-11-18 20:58:02 +00:00
|
|
|
if (v) {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.verbose(k, replaceInfo(v))
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-07-15 20:09:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:04:46 +00:00
|
|
|
log.verbose('cwd', process.cwd())
|
|
|
|
log.verbose('', os.type() + ' ' + os.release())
|
|
|
|
log.verbose('node', process.version)
|
|
|
|
log.verbose('npm ', 'v' + npm.version)
|
2021-07-15 20:09:18 +00:00
|
|
|
|
|
|
|
for (const k of ['code', 'syscall', 'file', 'path', 'dest', 'errno']) {
|
|
|
|
const v = err[k]
|
2021-11-18 20:58:02 +00:00
|
|
|
if (v) {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.error(k, v)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-07-15 20:09:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 22:38:23 -05:00
|
|
|
const { summary, detail, json, files = [] } = errorMessage(err, npm)
|
|
|
|
jsonError = json
|
2022-12-06 22:18:33 -05:00
|
|
|
|
|
|
|
for (let [file, content] of files) {
|
|
|
|
file = `${npm.logPath}${file}`
|
|
|
|
content = `'Log files:\n${npm.logFiles.join('\n')}\n\n${content.trim()}\n`
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(file, content)
|
|
|
|
detail.push(['', `\n\nFor a full report see:\n${file}`])
|
2023-01-16 22:38:23 -05:00
|
|
|
} catch (logFileErr) {
|
|
|
|
log.warn('', `Could not write error message to ${file} due to ${logFileErr}`)
|
2022-12-06 22:18:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const errline of [...summary, ...detail]) {
|
2021-12-02 22:04:46 +00:00
|
|
|
log.error(...errline)
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-07-15 20:09:18 +00:00
|
|
|
|
2021-11-18 20:58:02 +00:00
|
|
|
if (typeof err.errno === 'number') {
|
2021-07-15 20:09:18 +00:00
|
|
|
exitCode = err.errno
|
2021-11-18 20:58:02 +00:00
|
|
|
} else if (typeof err.code === 'number') {
|
2021-07-15 20:09:18 +00:00
|
|
|
exitCode = err.code
|
2021-11-18 20:58:02 +00:00
|
|
|
}
|
2021-07-15 20:09:18 +00:00
|
|
|
}
|
2020-10-02 17:52:19 -04:00
|
|
|
}
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
if (hasLoadedNpm) {
|
2024-04-30 23:53:22 -07:00
|
|
|
output.flush({ [META]: true, jsonError })
|
2022-12-06 22:18:33 -05:00
|
|
|
}
|
|
|
|
|
2021-12-02 22:04:46 +00:00
|
|
|
log.verbose('exit', exitCode || 0)
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2022-03-31 22:43:17 +00:00
|
|
|
showLogFileError = (hasLoadedNpm && npm.silent) || noLogMessage
|
2021-12-02 22:04:46 +00:00
|
|
|
? false
|
|
|
|
: !!exitCode
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2021-07-15 20:09:18 +00:00
|
|
|
// explicitly call process.exit now so we don't hang on things like the
|
2022-03-31 22:43:17 +00:00
|
|
|
// update notifier, also flush stdout/err beforehand because process.exit doesn't
|
2021-07-15 20:09:18 +00:00
|
|
|
// wait for that to happen.
|
2022-03-31 22:43:17 +00:00
|
|
|
let flushed = 0
|
|
|
|
const flush = [process.stderr, process.stdout]
|
|
|
|
const exit = () => ++flushed === flush.length && process.exit(exitCode)
|
|
|
|
flush.forEach((f) => f.write('', exit))
|
2011-11-21 09:48:45 -08:00
|
|
|
}
|
|
|
|
|
2021-06-24 21:39:48 +00:00
|
|
|
module.exports = exitHandler
|
2022-03-31 22:43:17 +00:00
|
|
|
module.exports.setNpm = n => (npm = n)
|