Skip to content

Commit 745f47e

Browse files
authored
Merge pull request #372 from ManiMatter/fix/issue-363-env-yaml-errors
[NEEDS CODE REVIEWER] fix: Clarify malformed environment YAML errors
2 parents 57a5c49 + 58d4d7c commit 745f47e

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

src/settings/_user_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ def _load_from_env() -> dict:
9393
parsed_value = _lowercase(parsed_value)
9494
except yaml.YAMLError as e:
9595
logger.error(
96-
f"Failed to parse environment variable {key} as YAML:\n{e}",
96+
"Could not parse %s as YAML. Check for smart quotes or "
97+
"incorrect indentation. Configuration from this variable "
98+
"was ignored.\n%s",
99+
key,
100+
e,
97101
)
98102
parsed_value = {}
99103
section_config[key.lower()] = parsed_value

tests/settings/test_user_config_from_env.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Test loading the user configuration from environment variables."""
22

3+
import logging
34
import os
45
import textwrap
56
from unittest.mock import patch
@@ -137,3 +138,20 @@ def test_env_loading_parametrized(
137138
assert value == expected
138139
else:
139140
assert value == expected
141+
142+
143+
def test_invalid_download_client_yaml_logs_actionable_error(caplog):
144+
malformed_qbit_yaml = '- base_url: "http://qbittorrent:8080'
145+
146+
with (
147+
patch.dict(os.environ, {"QBITTORRENT": malformed_qbit_yaml}, clear=True),
148+
caplog.at_level(logging.ERROR, logger="src.utils.log_setup"),
149+
):
150+
config = _load_from_env()
151+
152+
assert config["download_clients"]["qbittorrent"] == {}
153+
assert len(caplog.records) == 1
154+
message = caplog.records[0].getMessage()
155+
assert "Could not parse QBITTORRENT as YAML" in message
156+
assert "smart quotes or incorrect indentation" in message
157+
assert "Configuration from this variable was ignored" in message

0 commit comments

Comments
 (0)