Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Handle validation exceptions. #124

Merged
merged 7 commits into from
Apr 4, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Versioning](http://semver.org/spec/v2.0.0.html).
- Merge PR #130, implementing "safe-buffer" polyfill to be compatible with older NodeJS versions
- Merge PR #126, improving null value validation when null is passed into a template

### Changed
- Merge PR #124, adding the `--verbose` flag and returning exit code `1` on a fatal exception or parsing error

## [1.5.1] - 2018-03-12
### Added
- Merge PR #113, adding a Spanish translation for README.md
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Template invalid!
`--only-guess-parameters <param names>`: Only guess the provided parameters, and disable the guessing of all others without Defaults. A critical error will be raised for missing parameters, as above. e.g.
- `--only-guess-parameters InstanceType,Memory`

`--verbose`: Provide verbose output and stack traces when template parsing fails.

### What can cfn-lint do?
* Read JSON + YAML (Including YAML short form)
Expand Down Expand Up @@ -282,4 +283,4 @@ This document is available in:
- [English](https://github.com/martysweet/cfn-lint/blob/master/README.md)
- [Español](https://github.com/martysweet/cfn-lint/blob/master/README_es.md)

If you wish to translate this document to another language, we welcome your contribution!
If you wish to translate this document to another language, we welcome your contribution!
24 changes: 23 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ program
// .option('--guess-parameters', 'Guess any parameters that are not explicitely passed in and have no Default. This is the default behaviour.')
.option('-G, --no-guess-parameters', 'Fail validation if a parameter with no Default is not passed')
.option('-g, --only-guess-parameters <items>', 'Guess the provided parameters, and fail validation if a parameter with no Default is passed', list)
.option('-v, --verbose', 'Verbose error messages')
.action(function (arg1, arg2) {
firstArg = arg1;
secondArg = arg2;
Expand Down Expand Up @@ -74,7 +75,28 @@ if(firstArg == "validate"){
guessParameters
};

let result = validator.validateFile(secondArg, options);
let result = Object();
try {
result = validator.validateFile(secondArg, options);
} catch(err) {
let error: string = function(msg: string, errors: any) {
for (let error of Object.keys(errors)) {
if (RegExp(error).test(msg)) {
return errors[error];
}
}
return errors[''];
}(err.message, {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be indented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure... should it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently not 👍

'Could not find file .*. Check the input path.': 'No such file.',
'': 'Unable to parse template! Use --verbose for more information.'
});
console.log(error);
if (program.verbose) {
console.error(err);
}
process.exit(1);
}

// Show the errors
console.log((result['errors']['info'].length + " infos").grey);
for(let info of result['errors']['info']){
Expand Down
18 changes: 18 additions & 0 deletions src/test/indexTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ describe('index', () => {
done();
});
}).timeout(5000);

it('handle validation exceptions - non-verbose', (done) => {
exec('node lib/index.js validate testData/invalid/yaml/invalid_yaml.yaml', function(error, stdout, stderr) {
expect(error).to.have.property('code', 1);
expect(stdout).to.contain('Unable to parse template! Use --verbose for more information.');
expect(stderr).to.be.empty;
done();
});
}).timeout(5000);

it('handle validation exceptions - verbose', (done) => {
exec('node lib/index.js validate testData/invalid/yaml/invalid_yaml.yaml --verbose', function(error, stdout, stderr) {
expect(error).to.have.property('code', 1);
expect(stdout).to.contain('Unable to parse template! Use --verbose for more information.');
expect(stderr).to.contain('Error: Could not determine file type');
done();
});
}).timeout(5000);
});

});