fix(cli): gracefully output configuration validation errors #772
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
Improve handling of exceptions to provide a useful message to user without stack trace
Rationale
If a user puts in an invalid configuration setting, the configuration validator will throw a ValidationException that we do not catch gracefully. This causes python to dump a stack trace and hide the pretty ValidationError that pydantic provides by default.
In order to set up the test appropriately, I found it useful to add a fixture that will modify the configuration file accordingly. Meanwhile, I noticed that the implementation of example project was a bit fragile as exceptions are not handled well with generators or context managers unless explicitly using try/finally. With this modification I was able to align it more with the setup/teardown generator style fixtures as pytest recommends.
How I tested
Created a command line test that modifies the configuration file to be incorrect before executing the
version
command. It evaluates the exit code and the stderr message to the user that there is a validation error and which component.How to verify