Cleaner command line error output
See original GitHub issueIssue Description
Currently, when yamale is used from the command line and an error is found the output is difficult to parse since schema.validate() will raise and then command_line.main() will also raise. The raise() from command_line.main() doesn’t actually add anything new and the raise() from validate() includes the python stack it makes it hard to find the actual problem - I’m only really interested in what yamale found, not the methods it called to find them.
To deal with this locally, I made some very simple changes that I’d be happy to contribute back if you’re interested. In command_line.main(), I simply wrapped the call to _router():
try:
_router(args.path, args.schema, args.cpu_num, args.parser, args.strict)
except ValueError:
# Already processed
pass
else:
print('Validation success! 👍')
To get rid of the python stack from the output, I changed one line in command_line._validate() from
error += traceback.format_exc()
to error += str(e)
.
With those changes, I was able to change this output
Validating /Users/televi/foo.yaml...
Error!
Schema: bar.yaml
Data file: /Users/televi/foo.yaml
Traceback (most recent call last):
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 29, in _validate
yamale.validate(schema, data, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/yamale.py", line 38, in validate
schema.validate(d, path, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/schema/schema.py", line 65, in validate
raise ValueError(error_str)
ValueError:
Error validating data /Users/televi/foo.yaml with schema bar.yaml
borg: 'None' is not a str.
Traceback (most recent call last):
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 29, in _validate
yamale.validate(schema, data, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/yamale.py", line 38, in validate
schema.validate(d, path, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/schema/schema.py", line 65, in validate
raise ValueError(error_str)
ValueError:
Error validating data /Users/televi/foo.yaml with schema bar.yaml
borg: 'None' is not a str.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/televi/.venv/bin/yamale", line 11, in <module>
load_entry_point('yamale==2.0', 'console_scripts', 'yamale')()
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 115, in main
_router(args.path, args.schema, args.cpu_num, args.parser, args.strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 97, in _router
_validate_single(root, schema_name, parser, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 68, in _validate_single
_validate(s, yaml_path, parser, strict)
File "/Users/televi/.venv/lib/python3.6/site-packages/yamale/command_line.py", line 36, in _validate
raise ValueError('Validation failed!')
ValueError: Validation failed!
to this output
Validating /Users/televi/foo.yaml...
Error!
Schema: bar.yaml
Data file: /Users/televi/foo.yaml
Error validating data /Users/televi/foo.yaml with schema bar.yaml
borg: 'None' is not a str.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
In looking a bit more it may be useful to print an error in _validate_singleton() when the file isn’t found - else we may bury that error by mistake.
We could also simply print Validation failed! to provide a grepable string instead of passing or better yet exit with a non-zero code or both.
I agree, thanks!