Skip to content

Commit dfd629d

Browse files
authored
fix: Better error handling for main execution, reporting (#1229)
1 parent 549c953 commit dfd629d

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

bin/nyc.js

+26-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const configUtil = require('../lib/config-util')
5+
const { cliWrapper, suppressEPIPE } = require('../lib/commands/helpers')
56
const foreground = require('foreground-child')
67
const resolveFrom = require('resolve-from')
78
const NYC = require('../index.js')
@@ -81,29 +82,32 @@ async function main () {
8182
// a non-zero exit codes in either one leads to an overall non-zero exit code.
8283
process.exitCode = 0
8384
foreground(childArgs, async () => {
84-
var mainChildExitCode = process.exitCode
85-
86-
await nyc.writeProcessIndex()
87-
88-
nyc.maybePurgeSourceMapCache()
89-
if (argv.checkCoverage) {
90-
await nyc.checkCoverage({
91-
lines: argv.lines,
92-
functions: argv.functions,
93-
branches: argv.branches,
94-
statements: argv.statements
95-
}, argv['per-file'])
96-
process.exitCode = process.exitCode || mainChildExitCode
97-
}
98-
99-
if (!argv.silent) {
100-
await nyc.report()
85+
const mainChildExitCode = process.exitCode
86+
87+
try {
88+
await nyc.writeProcessIndex()
89+
90+
nyc.maybePurgeSourceMapCache()
91+
if (argv.checkCoverage) {
92+
await nyc.checkCoverage({
93+
lines: argv.lines,
94+
functions: argv.functions,
95+
branches: argv.branches,
96+
statements: argv.statements
97+
}, argv['per-file']).catch(suppressEPIPE)
98+
process.exitCode = process.exitCode || mainChildExitCode
99+
}
100+
101+
if (!argv.silent) {
102+
await nyc.report().catch(suppressEPIPE)
103+
}
104+
} catch (error) {
105+
/* istanbul ignore next */
106+
process.exitCode = process.exitCode || mainChildExitCode || 1
107+
/* istanbul ignore next */
108+
console.error(error.message)
101109
}
102110
})
103111
}
104112

105-
/* istanbul ignore next: the error branch should be unreachable */
106-
main().catch(error => {
107-
console.error(error.message)
108-
process.exit(1)
109-
})
113+
cliWrapper(main)()

lib/commands/check-coverage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const NYC = require('../../index.js')
4-
const { cliWrapper, setupOptions } = require('./helpers.js')
4+
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')
55

66
exports.command = 'check-coverage'
77

@@ -24,5 +24,5 @@ exports.handler = cliWrapper(async argv => {
2424
functions: argv.functions,
2525
branches: argv.branches,
2626
statements: argv.statements
27-
}, argv['per-file'])
27+
}, argv['per-file']).catch(suppressEPIPE)
2828
})

lib/commands/helpers.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,23 @@ module.exports = {
5050
}
5151
})
5252
},
53+
/* istanbul ignore next: unsure how to test this */
54+
suppressEPIPE (error) {
55+
/* Prevent dumping error when `nyc npm t|head` causes stdout to
56+
* be closed when reporting runs. */
57+
if (error.code !== 'EPIPE') {
58+
throw error
59+
}
60+
},
5361
cliWrapper (execute) {
5462
return argv => {
5563
execute(argv).catch(error => {
56-
console.error(error.message)
64+
try {
65+
console.error(error.message)
66+
} catch (_) {
67+
/* We need to run process.exit(1) even if stderr is destroyed */
68+
}
69+
5770
process.exit(1)
5871
})
5972
}

lib/commands/report.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const NYC = require('../../index.js')
4-
const { cliWrapper, setupOptions } = require('./helpers.js')
4+
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')
55

66
exports.command = 'report'
77

@@ -18,13 +18,13 @@ exports.builder = function (yargs) {
1818
exports.handler = cliWrapper(async argv => {
1919
process.env.NYC_CWD = process.cwd()
2020
var nyc = new NYC(argv)
21-
await nyc.report()
21+
await nyc.report().catch(suppressEPIPE)
2222
if (argv.checkCoverage) {
2323
await nyc.checkCoverage({
2424
lines: argv.lines,
2525
functions: argv.functions,
2626
branches: argv.branches,
2727
statements: argv.statements
28-
}, argv['per-file'])
28+
}, argv['per-file']).catch(suppressEPIPE)
2929
}
3030
})

0 commit comments

Comments
 (0)