-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Improve error messages for yaml and json loading errors #2601
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import json | ||
| import math | ||
|
|
||
| from .exception import CurriculumError | ||
| from .exception import CurriculumConfigError, CurriculumLoadingError | ||
|
|
||
| import logging | ||
|
|
||
|
|
@@ -23,14 +23,8 @@ def __init__(self, location, default_reset_parameters): | |
| # The name of the brain should be the basename of the file without the | ||
| # extension. | ||
| self._brain_name = os.path.basename(location).split(".")[0] | ||
| self.data = Curriculum.load_curriculum_file(location) | ||
|
|
||
| try: | ||
| with open(location) as data_file: | ||
| self.data = json.load(data_file) | ||
| except IOError: | ||
| raise CurriculumError("The file {0} could not be found.".format(location)) | ||
| except UnicodeDecodeError: | ||
| raise CurriculumError("There was an error decoding {}".format(location)) | ||
| self.smoothing_value = 0 | ||
| for key in [ | ||
| "parameters", | ||
|
|
@@ -40,7 +34,7 @@ def __init__(self, location, default_reset_parameters): | |
| "signal_smoothing", | ||
| ]: | ||
| if key not in self.data: | ||
| raise CurriculumError( | ||
| raise CurriculumConfigError( | ||
| "{0} does not contain a " "{1} field.".format(location, key) | ||
| ) | ||
| self.smoothing_value = 0 | ||
|
|
@@ -51,12 +45,12 @@ def __init__(self, location, default_reset_parameters): | |
| parameters = self.data["parameters"] | ||
| for key in parameters: | ||
| if key not in default_reset_parameters: | ||
| raise CurriculumError( | ||
| raise CurriculumConfigError( | ||
| "The parameter {0} in Curriculum {1} is not present in " | ||
| "the Environment".format(key, location) | ||
| ) | ||
| if len(parameters[key]) != self.max_lesson_num + 1: | ||
| raise CurriculumError( | ||
| raise CurriculumConfigError( | ||
| "The parameter {0} in Curriculum {1} must have {2} values " | ||
| "but {3} were found".format( | ||
| key, location, self.max_lesson_num + 1, len(parameters[key]) | ||
|
|
@@ -117,3 +111,27 @@ def get_config(self, lesson=None): | |
| for key in parameters: | ||
| config[key] = parameters[key][lesson] | ||
| return config | ||
|
|
||
| @staticmethod | ||
| def load_curriculum_file(location): | ||
| try: | ||
| with open(location) as data_file: | ||
| return Curriculum._load_curriculum(data_file) | ||
| except IOError: | ||
| raise CurriculumLoadingError( | ||
| "The file {0} could not be found.".format(location) | ||
| ) | ||
| except UnicodeDecodeError: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be handled in _load_curriculum instead, but I couldn't reproduce it to confirm :/ |
||
| raise CurriculumLoadingError( | ||
| "There was an error decoding {}".format(location) | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _load_curriculum(fp): | ||
| try: | ||
| return json.load(fp) | ||
| except json.decoder.JSONDecodeError as e: | ||
| raise CurriculumLoadingError( | ||
| "Error parsing JSON file. Please check for formatting errors. " | ||
| "A tool such as https://jsonlint.com/ can be helpful with this." | ||
| ) from e | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept these in the same file, but I can move them to trainer_util.py if that's cleaner. It's currently the only place we load json.