Skip to content

Commit c73b30b

Browse files
committed
Add config file info to backend errors (#627)
* Improve backend error message * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci
1 parent 55a2d63 commit c73b30b

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

servicex/configuration.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class Configuration(BaseModel):
4949
)
5050

5151
shortened_downloaded_filename: Optional[bool] = False
52+
# Path to the configuration file this object was read from. This field is
53+
# populated by :py:meth:`Configuration.read` and is not part of the input
54+
# schema.
55+
config_file: Optional[str] = Field(default=None, exclude=True)
5256

5357
@model_validator(mode="after")
5458
def expand_cache_path(self):
@@ -96,12 +100,17 @@ def read(cls, config_path: Optional[str] = None):
96100
:return: Populated configuration object
97101
"""
98102
if config_path:
99-
yaml_config = cls._add_from_path(Path(config_path), walk_up_tree=False)
103+
yaml_config, cfg_path = cls._add_from_path(
104+
Path(config_path), walk_up_tree=False
105+
)
100106
else:
101-
yaml_config = cls._add_from_path(walk_up_tree=True)
107+
yaml_config, cfg_path = cls._add_from_path(walk_up_tree=True)
102108

103109
if yaml_config:
104-
return Configuration.model_validate(yaml_config)
110+
cfg = Configuration.model_validate(yaml_config)
111+
if cfg_path:
112+
cfg.config_file = str(cfg_path)
113+
return cfg
105114
else:
106115
path_extra = f"in {config_path}" if config_path else ""
107116
raise NameError(
@@ -111,6 +120,7 @@ def read(cls, config_path: Optional[str] = None):
111120
@classmethod
112121
def _add_from_path(cls, path: Optional[Path] = None, walk_up_tree: bool = False):
113122
config = None
123+
found_file: Optional[Path] = None
114124
if path:
115125
path.resolve()
116126
name = path.name
@@ -126,14 +136,16 @@ def _add_from_path(cls, path: Optional[Path] = None, walk_up_tree: bool = False)
126136
if f.exists():
127137
with open(f) as config_file:
128138
config = yaml.safe_load(config_file)
129-
break
139+
found_file = f
140+
break
130141

131142
if alt_name:
132143
f = dir / alt_name # if neither option above, find servicex.yaml
133144
if f.exists():
134145
with open(f) as config_file:
135146
config = yaml.safe_load(config_file)
136-
break
147+
found_file = f
148+
break
137149

138150
if not walk_up_tree:
139151
break
@@ -155,6 +167,7 @@ def _add_from_path(cls, path: Optional[Path] = None, walk_up_tree: bool = False)
155167
if f.exists():
156168
with open(f) as config_file:
157169
config = yaml.safe_load(config_file)
170+
found_file = f
158171
break
159172

160-
return config
173+
return config, found_file

servicex/servicex_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,9 @@ def __init__(self, backend=None, url=None, config_path=None):
344344
elif backend:
345345
if backend not in self.endpoints:
346346
valid_backends = ", ".join(self.endpoints.keys())
347+
cfg_file = self.config.config_file or ".servicex"
347348
raise ValueError(
348-
f"Backend {backend} not defined in .servicex file. "
349+
f"Backend {backend} not defined in {cfg_file} file. "
349350
f"Valid backend names: {valid_backends}"
350351
)
351352
self.servicex = ServiceXAdapter(

tests/test_servicex_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
from datetime import datetime
29+
from pathlib import Path
2930
from unittest.mock import MagicMock, patch
3031

3132
import pytest
@@ -138,3 +139,13 @@ def test_delete_transform_from_cache(mock_cache, servicex_adaptor, transformed_r
138139
mock_cache.return_value.delete_record_by_request_id.assert_called_once_with(
139140
"servicex-request-789"
140141
)
142+
143+
144+
def test_invalid_backend_raises_error_with_filename():
145+
config_file = "tests/example_config.yaml"
146+
expected = Path(config_file).resolve()
147+
148+
with pytest.raises(ValueError) as err:
149+
ServiceXClient(backend="badname", config_path=config_file)
150+
151+
assert f"Backend badname not defined in {expected} file" in str(err.value)

0 commit comments

Comments
 (0)