Skip to content

Commit 4d6f165

Browse files
authored
fix: Hide err msg in private field to avoid duplicate logs (#421)
1 parent 3ea8b06 commit 4d6f165

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/printErrors.js

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

3+
// Work-around for duplicated error logs, see #142
4+
const errMsg = err => (err.privateMsg != null ? err.privateMsg : err.message)
5+
36
module.exports = function printErrors(errorInstance) {
47
if (Array.isArray(errorInstance.errors)) {
58
errorInstance.errors.forEach(lintError => {
6-
console.error(lintError.message)
9+
console.error(errMsg(lintError))
710
})
811
} else {
9-
console.error(errorInstance.message)
12+
console.error(errMsg(errorInstance))
1013
}
1114
}

src/runScript.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ module.exports = function runScript(commands, pathsToLint, config) {
6060
const errStdout = errors.map(err => err.stdout).join('')
6161
const errStderr = errors.map(err => err.stderr).join('')
6262

63-
// prettier-ignore
64-
throw new Error(dedent`
65-
${logSymbols.error} ${linter} found some errors. Please fix them and try committing again.
66-
${errStdout}
67-
${errStderr}
68-
`)
63+
// If we set the message on the error instance, it gets logged
64+
// multiple times(see #142). So we set the actual error message in a
65+
// private field and extract it later, log only once.
66+
const err = new Error()
67+
err.privateMsg = dedent`
68+
${
69+
logSymbols.error
70+
} "${linter}" found some errors. Please fix them and try committing again.
71+
${errStdout}
72+
${errStderr}
73+
`
74+
75+
throw err
6976
})
7077
}
7178
}))

test/runScript.spec.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ describe('runScript', () => {
133133
try {
134134
await linter.task()
135135
} catch (err) {
136-
// prettier-ignore
137-
expect(err.message).toMatch(dedent`
138-
${logSymbols.error} mock-fail-linter found some errors. Please fix them and try committing again.
136+
expect(err.privateMsg).toMatch(dedent`
137+
${
138+
logSymbols.error
139+
} "mock-fail-linter" found some errors. Please fix them and try committing again.
139140
Mock error
140141
`)
141142
}

0 commit comments

Comments
 (0)