Skip to content

Commit 3131bdf

Browse files
authored
Merge pull request #220 from illuin-tech/better-error-messages
Improve error messages when loading fails
2 parents 3a24973 + 771b986 commit 3131bdf

File tree

5 files changed

+22
-10
lines changed

5 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## 6.0.1
3+
### Fixes
4+
- Improve error messages when loading fails
5+
26
## 6.0.0
37
### Breaking changes
48
- Remove support for Python 3.8

configue/configue_loader.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import re
44
from collections.abc import Hashable
5-
from typing import Any, List, Mapping, Union
5+
from typing import Any, List, Mapping, Optional, Union
66

77
import yaml
88
from yaml.constructor import ConstructorError
@@ -25,6 +25,7 @@ def construct_yaml_map(self, node: yaml.MappingNode) -> Any:
2525
path = mapping.pop(CONSTRUCTOR_KEY)
2626
object_path_elements = path.split(".")
2727
remaining_path_elements: List[str] = []
28+
exceptions: list[Optional[str]] = []
2829
while object_path_elements:
2930
try:
3031
cls = self.find_python_name(
@@ -33,10 +34,13 @@ def construct_yaml_map(self, node: yaml.MappingNode) -> Any:
3334
unsafe=True,
3435
)
3536
break
36-
except ConstructorError:
37+
except ConstructorError as error:
38+
exceptions.append(error.problem)
3739
remaining_path_elements.insert(0, object_path_elements.pop(-1))
3840
else:
39-
raise NotFoundError(f"Could not load element {path} {node.start_mark}")
41+
raise NotFoundError(
42+
f"Could not load element {path} {node.start_mark}, exceptions encountered: {exceptions!r}"
43+
) from None
4044
for path_element in remaining_path_elements:
4145
cls = getattr(cls, path_element)
4246

configue/file_loader.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def _load_ext(self, loader: ConfigueLoader, node: ScalarNode) -> Any:
120120
path = loader.construct_scalar(node)
121121
object_path_elements = path.split(".")
122122
remaining_path_elements: List[str] = []
123+
exceptions: list[Optional[str]] = []
123124
while object_path_elements:
124125
try:
125126
loaded_object = loader.find_python_name(
@@ -128,10 +129,13 @@ def _load_ext(self, loader: ConfigueLoader, node: ScalarNode) -> Any:
128129
unsafe=True,
129130
)
130131
break
131-
except ConstructorError:
132+
except ConstructorError as error:
133+
exceptions.append(error.problem)
132134
remaining_path_elements.insert(0, object_path_elements.pop(-1))
133135
else:
134-
raise NotFoundError(f"Could not load element {path} {node.start_mark}")
136+
raise NotFoundError(
137+
f"Could not load element {path} {node.start_mark}, encountered exceptions: {exceptions!r}"
138+
) from None
135139
remaining_path = ".".join(remaining_path_elements)
136140
if remaining_path:
137141
return self._get_element_at_sub_path(remaining_path, loaded_object)

tests/test_configue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from unittest import TestCase
44

55
import configue
6-
from configue.exceptions import NonCallableError, SubPathNotFound, NotFoundError
6+
from configue.exceptions import ConfigueError, NonCallableError, SubPathNotFound, NotFoundError
77
from tests.external_module import CONSTANT, MyObject, Color
88

99

@@ -178,11 +178,11 @@ def test_ext_raises_exception_on_submodule_not_found(self):
178178
configue.load(self._get_path("test_file_2.yml"), "invalid_ext.wrong_sub_module")
179179

180180
def test_ext_raises_exception_on_element_not_found(self):
181-
with self.assertRaises(NotFoundError):
181+
with self.assertRaises(ConfigueError):
182182
configue.load(self._get_path("test_file_2.yml"), "invalid_ext.wrong_element")
183183

184184
def test_ext_raises_exception_on_property_not_found(self):
185-
with self.assertRaises(NotFoundError):
185+
with self.assertRaises(KeyError):
186186
configue.load(self._get_path("test_file_2.yml"), "invalid_ext.wrong_property")
187187

188188
def test_loading_empty_file(self):

tests/test_file_2.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ enum_loading: !ext tests.external_module.Color.RED
3737
invalid_ext:
3838
wrong_module: !ext invalid.test.Color.RED
3939
wrong_sub_module: !ext tests.invalid.Color.RED
40-
wrong_element: !ext tests.invalid.Invalid.RED
41-
wrong_property: !ext tests.invalid.Color.GREEN
40+
wrong_element: !ext tests.external_module.Invalid.RED
41+
wrong_property: !ext tests.external_module.Color.GREEN
4242

4343
static_loading:
4444
(): tests.external_module.Static.get_static_value

0 commit comments

Comments
 (0)