khaeru / sdmx

SDMX information model and client in Python
https://sdmx1.readthedocs.io
Apache License 2.0
23 stars 17 forks source link

Provide more logging and raise exception when xml file is empty #172

Open edkry opened 2 months ago

edkry commented 2 months ago
ecb = sdmx.Client("ECB", backend="memory")
params = dict(startPeriod="2024")
data_msg = ecb.data("ICB", params=params)

This runs but gives an error:

File [~/.local/lib/python3.10/site-packages/sdmx/client.py:496](https://vscode-remote+wsl-002bubuntu.vscode-resource.vscode-cdn.net/home/ek/DataLab/lsd/~/.local/lib/python3.10/site-packages/sdmx/client.py:496) in get
    msg = reader.read_message(response_content, structure=kwargs.get("dsd", None))
...
...
...
XMLSyntaxError: no element found

What happens is that ICB table is empty for 2024: https://data-api.ecb.europa.eu/service/data/ICB?startPeriod=2024

My suggestion is to add additional check before using the reader to read message:

if len(response_content) == 0: 
    raise exception(with meaningful text)
msg = reader.read_message(response_content, structure=kwargs.get("dsd", None))
khaeru commented 2 months ago

Hi—thanks, this would indeed be a nice convenience feature for users. A PR would be welcome.

Could you expand a bit more about where specifically you'd like more logging? There are already many log messages created. Which code should log which information?

edkry commented 2 months ago

I can make a PR for sure, would be happy to.

I am thinking what would be the best approach. Logging is not that necessary, raising an exception would be nice, so you can catch it. However, it would be better to return just None if len(response_content) == 0.

What do you think?

edkry commented 2 months ago

Opened a PR to start a discussion on possible change.

https://github.com/khaeru/sdmx/pull/174