Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better logging for js config errors #935

Merged
merged 3 commits into from Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})