Skip to content

Commit

Permalink
fix: Better error handling for main execution, reporting (#1229)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed Nov 19, 2019
1 parent 549c953 commit dfd629d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 28 deletions.
48 changes: 26 additions & 22 deletions bin/nyc.js
Expand Up @@ -2,6 +2,7 @@
'use strict'

const configUtil = require('../lib/config-util')
const { cliWrapper, suppressEPIPE } = require('../lib/commands/helpers')
const foreground = require('foreground-child')
const resolveFrom = require('resolve-from')
const NYC = require('../index.js')
Expand Down Expand Up @@ -81,29 +82,32 @@ async function main () {
// a non-zero exit codes in either one leads to an overall non-zero exit code.
process.exitCode = 0
foreground(childArgs, async () => {
var mainChildExitCode = process.exitCode

await nyc.writeProcessIndex()

nyc.maybePurgeSourceMapCache()
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
process.exitCode = process.exitCode || mainChildExitCode
}

if (!argv.silent) {
await nyc.report()
const mainChildExitCode = process.exitCode

try {
await nyc.writeProcessIndex()

nyc.maybePurgeSourceMapCache()
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file']).catch(suppressEPIPE)
process.exitCode = process.exitCode || mainChildExitCode
}

if (!argv.silent) {
await nyc.report().catch(suppressEPIPE)
}
} catch (error) {
/* istanbul ignore next */
process.exitCode = process.exitCode || mainChildExitCode || 1
/* istanbul ignore next */
console.error(error.message)
}
})
}

/* istanbul ignore next: the error branch should be unreachable */
main().catch(error => {
console.error(error.message)
process.exit(1)
})
cliWrapper(main)()
4 changes: 2 additions & 2 deletions lib/commands/check-coverage.js
@@ -1,7 +1,7 @@
'use strict'

const NYC = require('../../index.js')
const { cliWrapper, setupOptions } = require('./helpers.js')
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')

exports.command = 'check-coverage'

Expand All @@ -24,5 +24,5 @@ exports.handler = cliWrapper(async argv => {
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}, argv['per-file']).catch(suppressEPIPE)
})
15 changes: 14 additions & 1 deletion lib/commands/helpers.js
Expand Up @@ -50,10 +50,23 @@ module.exports = {
}
})
},
/* istanbul ignore next: unsure how to test this */
suppressEPIPE (error) {
/* Prevent dumping error when `nyc npm t|head` causes stdout to
* be closed when reporting runs. */
if (error.code !== 'EPIPE') {
throw error
}
},
cliWrapper (execute) {
return argv => {
execute(argv).catch(error => {
console.error(error.message)
try {
console.error(error.message)
} catch (_) {
/* We need to run process.exit(1) even if stderr is destroyed */
}

process.exit(1)
})
}
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/report.js
@@ -1,7 +1,7 @@
'use strict'

const NYC = require('../../index.js')
const { cliWrapper, setupOptions } = require('./helpers.js')
const { cliWrapper, suppressEPIPE, setupOptions } = require('./helpers.js')

exports.command = 'report'

Expand All @@ -18,13 +18,13 @@ exports.builder = function (yargs) {
exports.handler = cliWrapper(async argv => {
process.env.NYC_CWD = process.cwd()
var nyc = new NYC(argv)
await nyc.report()
await nyc.report().catch(suppressEPIPE)
if (argv.checkCoverage) {
await nyc.checkCoverage({
lines: argv.lines,
functions: argv.functions,
branches: argv.branches,
statements: argv.statements
}, argv['per-file'])
}, argv['per-file']).catch(suppressEPIPE)
}
})

0 comments on commit dfd629d

Please sign in to comment.