Skip to content

Commit

Permalink
fix: Hide err msg in private field to avoid duplicate logs (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-suhas committed Apr 1, 2018
1 parent 3ea8b06 commit 4d6f165
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/printErrors.js
@@ -1,11 +1,14 @@
'use strict'

// Work-around for duplicated error logs, see #142
const errMsg = err => (err.privateMsg != null ? err.privateMsg : err.message)

module.exports = function printErrors(errorInstance) {
if (Array.isArray(errorInstance.errors)) {
errorInstance.errors.forEach(lintError => {
console.error(lintError.message)
console.error(errMsg(lintError))
})
} else {
console.error(errorInstance.message)
console.error(errMsg(errorInstance))
}
}
19 changes: 13 additions & 6 deletions src/runScript.js
Expand Up @@ -60,12 +60,19 @@ module.exports = function runScript(commands, pathsToLint, config) {
const errStdout = errors.map(err => err.stdout).join('')
const errStderr = errors.map(err => err.stderr).join('')

// prettier-ignore
throw new Error(dedent`
${logSymbols.error} ${linter} found some errors. Please fix them and try committing again.
${errStdout}
${errStderr}
`)
// If we set the message on the error instance, it gets logged
// multiple times(see #142). So we set the actual error message in a
// private field and extract it later, log only once.
const err = new Error()
err.privateMsg = dedent`
${
logSymbols.error
} "${linter}" found some errors. Please fix them and try committing again.
${errStdout}
${errStderr}
`

throw err
})
}
}))
Expand Down
7 changes: 4 additions & 3 deletions test/runScript.spec.js
Expand Up @@ -133,9 +133,10 @@ describe('runScript', () => {
try {
await linter.task()
} catch (err) {
// prettier-ignore
expect(err.message).toMatch(dedent`
${logSymbols.error} mock-fail-linter found some errors. Please fix them and try committing again.
expect(err.privateMsg).toMatch(dedent`
${
logSymbols.error
} "mock-fail-linter" found some errors. Please fix them and try committing again.
Mock error
`)
}
Expand Down

0 comments on commit 4d6f165

Please sign in to comment.