Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix(bundler): degrade non-UTF-8 config reads into BundlerError (#3784)

Ali jawwadbe33d2a5f6b9
diff --git a/src/specify_cli/bundler/lib/yamlio.py b/src/specify_cli/bundler/lib/yamlio.py
index 5143a1f68..a63d05ba4 100644
--- a/src/specify_cli/bundler/lib/yamlio.py
+++ b/src/specify_cli/bundler/lib/yamlio.py
@@ -58,7 +58,12 @@ def load_yaml(path: Path) -> Any:
raise BundlerError(f"File not found: {path}")
try:
text = path.read_text(encoding="utf-8")
- except OSError as exc:
+ except (OSError, UnicodeError) as exc:
+ # A non-UTF-8 file raises UnicodeDecodeError, which is a ValueError --
+ # NOT an OSError -- so it escaped this module's "IO failures degrade
+ # into actionable BundlerError" contract as a raw traceback. Realistic
+ # on Windows, where PowerShell 5.1's `Out-File`/`>` default to UTF-16.
+ # Matches the sibling catalog readers (catalogs.py, workflows/catalog.py).
raise BundlerError(f"Could not read {path}: {exc}") from exc
try:
has_node = yaml.compose(text) is not None
@@ -98,9 +103,15 @@ def load_json(path: Path) -> Any:
try:
with path.open("r", encoding="utf-8") as handle:
return json.load(handle)
+ # JSONDecodeError stays FIRST: it and UnicodeDecodeError are sibling
+ # ValueError subclasses (neither subsumes the other), so malformed-but-
+ # decodable JSON keeps its more specific "Invalid JSON" message while a
+ # decode failure falls through to the read-error clause below.
except json.JSONDecodeError as exc:
raise BundlerError(f"Invalid JSON in {path}: {exc}") from exc
- except OSError as exc:
+ except (OSError, UnicodeError) as exc:
+ # See load_yaml: a non-UTF-8 file raises UnicodeDecodeError, which is
+ # not an OSError, and previously escaped as a raw traceback.
raise BundlerError(f"Could not read {path}: {exc}") from exc
diff --git a/tests/unit/test_bundler_yamlio.py b/tests/unit/test_bundler_yamlio.py
index e9c60d762..b3e8e592e 100644
--- a/tests/unit/test_bundler_yamlio.py
+++ b/tests/unit/test_bundler_yamlio.py
@@ -3,7 +3,10 @@ from __future__ import annotations
from pathlib import Path
-from specify_cli.bundler.lib.yamlio import dump_yaml, load_yaml
+import pytest
+
+from specify_cli.bundler import BundlerError
+from specify_cli.bundler.lib.yamlio import dump_yaml, load_json, load_yaml
def test_dump_yaml_preserves_unicode(tmp_path: Path):
@@ -24,3 +27,32 @@ def test_dump_yaml_round_trips_unicode(tmp_path: Path):
data = {"note": "café", "city": "münchen"}
dump_yaml(path, data)
assert load_yaml(path) == data
+
+
+def test_load_yaml_non_utf8_raises_bundler_error(tmp_path: Path):
+ """A non-UTF-8 file must degrade into BundlerError, per this module's
+ documented contract. UnicodeDecodeError is a ValueError, not an OSError, so
+ it previously escaped as a raw traceback. UTF-16 is the realistic case:
+ PowerShell 5.1's `Out-File`/`>` default to it."""
+ path = tmp_path / "bundle-catalogs.yml"
+ path.write_bytes('catalogs: []\n'.encode("utf-16"))
+ with pytest.raises(BundlerError, match="Could not read"):
+ load_yaml(path)
+
+
+def test_load_json_non_utf8_raises_bundler_error(tmp_path: Path):
+ """Same for the JSON reader: json.JSONDecodeError is a *sibling* of
+ UnicodeDecodeError, so it does not cover a decode failure."""
+ path = tmp_path / "records.json"
+ path.write_bytes('{"bundles": []}'.encode("utf-16"))
+ with pytest.raises(BundlerError, match="Could not read"):
+ load_json(path)
+
+
+def test_load_json_malformed_still_reports_invalid_json(tmp_path: Path):
+ """Clause order regression guard: decodable-but-malformed JSON must keep the
+ more specific 'Invalid JSON' message rather than the read-error one."""
+ path = tmp_path / "records.json"
+ path.write_text('{"bundles": [', encoding="utf-8")
+ with pytest.raises(BundlerError, match="Invalid JSON"):
+ load_json(path)