Skip to content

Commit

Permalink
fix: better logging for errors in js config files (#935)
Browse files Browse the repository at this point in the history
Before this change javascript config files that had compilation errors or that threw their own errors wouldn't report the error, making debugging a mite more difficult than it should be: instead you'd get a useless "TypeError: Cannot read property 'errors' of undefined" message.  Now if the javascript config file tosses its cookies for whatever reason we can read what that reason was.

Co-authored-by: Ricky Curtice <kf6kjg@users.noreply.github.com>
  • Loading branch information
iiroj and kf6kjg committed Dec 4, 2020
1 parent 0ff2917 commit 292e882
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/index.js
Expand Up @@ -128,19 +128,24 @@ module.exports = async function lintStaged(
printTaskOutput(ctx, logger)
return true
} catch (runAllError) {
const { ctx } = runAllError
if (ctx.errors.has(ApplyEmptyCommitError)) {
logger.warn(PREVENTED_EMPTY_COMMIT)
} else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) {
logger.error(GIT_ERROR)
if (ctx.shouldBackup) {
// No sense to show this if the backup stash itself is missing.
logger.error(RESTORE_STASH_EXAMPLE)
if (runAllError && runAllError.ctx && runAllError.ctx.errors) {
const { ctx } = runAllError
if (ctx.errors.has(ApplyEmptyCommitError)) {
logger.warn(PREVENTED_EMPTY_COMMIT)
} else if (ctx.errors.has(GitError) && !ctx.errors.has(GetBackupStashError)) {
logger.error(GIT_ERROR)
if (ctx.shouldBackup) {
// No sense to show this if the backup stash itself is missing.
logger.error(RESTORE_STASH_EXAMPLE)
}
}

printTaskOutput(ctx, logger)
return false
}

printTaskOutput(ctx, logger)
return false
// Probably a compilation error in the config js file. Pass it up to the outer error handler for logging.
throw runAllError
}
} catch (lintStagedError) {
if (lintStagedError === errConfigNotFound) {
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/index2.spec.js.snap
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lintStaged should catch errors from js function config 2`] = `
"
ERROR Could not parse lint-staged config.
Error: failed config
ERROR
ERROR Please make sure you have created it correctly.
See https://github.com/okonet/lint-staged#configuration."
`;
15 changes: 15 additions & 0 deletions test/index2.spec.js
Expand Up @@ -64,4 +64,19 @@ describe('lintStaged', () => {
}
`)
})

it('should catch errors from js function config', async () => {
const logger = makeConsoleMock()
const config = {
'*': () => {
throw new Error('failed config')
},
}

expect.assertions(2)
await expect(lintStaged({ config }, logger)).rejects.toThrowErrorMatchingInlineSnapshot(
`"failed config"`
)
expect(logger.printHistory()).toMatchSnapshot()
})
})

0 comments on commit 292e882

Please sign in to comment.