Skip to content

Commit

Permalink
improve error msg when handling OmegaConfBaseException in config_load…
Browse files Browse the repository at this point in the history
…er (#1824)
  • Loading branch information
Jasha10 authored Sep 16, 2021
1 parent 498e404 commit f147d1b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
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

0 comments on commit f147d1b

Please sign in to comment.