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

improve error msg when handling OmegaConfBaseException in config_loader #1824

Merged
merged 4 commits into from
Sep 16, 2021
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
5 changes: 2 additions & 3 deletions hydra/_internal/config_loader_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
ConfigAttributeError,
ConfigKeyError,
OmegaConfBaseException,
ValidationError,
)

from hydra._internal.config_repository import (
Expand Down Expand Up @@ -517,9 +516,9 @@ def _compose_config_from_defaults_list(
loaded = self._load_single_config(default=default, repo=repo)
try:
cfg.merge_with(loaded.config)
except ValidationError as e:
except OmegaConfBaseException as e:
raise ConfigCompositionException(
f"In '{default.config_path}': Validation error while composing config:\n{e}"
f"In '{default.config_path}': {type(e).__name__} raised while composing config:\n{e}"
).with_traceback(sys.exc_info()[2])

# # remove remaining defaults lists from all nodes.
Expand Down
5 changes: 5 additions & 0 deletions hydra/test_utils/configs/schema_key_error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defaults:
- base_mysql
- _self_

foo: not_in_schema
5 changes: 5 additions & 0 deletions hydra/test_utils/configs/schema_validation_error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defaults:
- base_mysql
- _self_

port: not_an_int
1 change: 1 addition & 0 deletions news/1697.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve error message with more context when an omegaconf exception occurs during the config merge step.
50 changes: 50 additions & 0 deletions tests/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,56 @@ def test_load_config_file_with_schema_validation(
},
}

def test_load_config_with_validation_error(
self, hydra_restore_singletons: Any, path: str
) -> None:

ConfigStore.instance().store(
name="base_mysql", node=MySQLConfig, provider="this_test"
)
config_loader = ConfigLoaderImpl(
config_search_path=create_config_search_path(path)
)

msg = dedent(
"""\
In 'schema_validation_error': ValidationError raised while composing config:
Value 'not_an_int' could not be converted to Integer
full_key: port
object_type=MySQLConfig"""
)
with raises(ConfigCompositionException, match=re.escape(msg)):
config_loader.load_configuration(
config_name="schema_validation_error",
overrides=[],
run_mode=RunMode.RUN,
)

def test_load_config_with_key_error(
self, hydra_restore_singletons: Any, path: str
) -> None:

ConfigStore.instance().store(
name="base_mysql", node=MySQLConfig, provider="this_test"
)
config_loader = ConfigLoaderImpl(
config_search_path=create_config_search_path(path)
)

msg = dedent(
"""\
In 'schema_key_error': ConfigKeyError raised while composing config:
Key 'foo' not in 'MySQLConfig'
full_key: foo
object_type=MySQLConfig"""
)
with raises(ConfigCompositionException, match=re.escape(msg)):
config_loader.load_configuration(
config_name="schema_key_error",
overrides=[],
run_mode=RunMode.RUN,
)

def test_assign_null(self, hydra_restore_singletons: Any, path: str) -> None:
config_loader = ConfigLoaderImpl(
config_search_path=create_config_search_path(path)
Expand Down