Skip to content

Commit fe5e37a

Browse files
include module name in unsupported-resource error message
When a .plain file references a binary file, the error now names the module the reference came from, e.g. "Referenced resource 'icon.png' in module 'main' is a binary file."
1 parent 3812c6b commit fe5e37a

3 files changed

Lines changed: 7 additions & 6 deletions

File tree

file_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def open_from(dirs, file_name):
223223
return None
224224

225225

226-
def load_linked_resources(template_dirs: list[str], resources_list):
226+
def load_linked_resources(template_dirs: list[str], resources_list, module_name: str):
227227
linked_resources = {}
228228

229229
for resource in resources_list:
@@ -235,7 +235,7 @@ def load_linked_resources(template_dirs: list[str], resources_list):
235235
content = open_from(template_dirs, file_name)
236236
except UnicodeDecodeError:
237237
raise UnsupportedResourceType(
238-
f"Referenced resource '{file_name}' is a binary file. "
238+
f"Referenced resource '{file_name}' in module '{module_name}' is a binary file. "
239239
f"Only text files (e.g. .md, .txt, .json, .yaml) can be referenced from a .plain file."
240240
)
241241

render_machine/render_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def __init__(
8585

8686
resources_list = []
8787
plain_spec.collect_linked_resources(plain_source_tree, resources_list, None, True)
88-
self.all_linked_resources = file_utils.load_linked_resources(template_dirs, resources_list)
88+
self.all_linked_resources = file_utils.load_linked_resources(template_dirs, resources_list, module_name)
8989

9090
# Initialize context objects
9191
self.frid_context: Optional[FridContext] = None

tests/test_file_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_load_linked_resources_text_file(template_dir):
1818
with open(file_path, "w") as f:
1919
f.write("# hello")
2020

21-
result = load_linked_resources([template_dir], [{"text": "Notes", "target": "notes.md"}])
21+
result = load_linked_resources([template_dir], [{"text": "Notes", "target": "notes.md"}], "my_thing")
2222

2323
assert result == {"notes.md": "# hello"}
2424

@@ -29,12 +29,13 @@ def test_load_linked_resources_binary_file_raises_unsupported_resource_type(temp
2929
f.write(b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\xff\xfe\xfd")
3030

3131
with pytest.raises(UnsupportedResourceType) as exc_info:
32-
load_linked_resources([template_dir], [{"text": "Icon", "target": "icon.png"}])
32+
load_linked_resources([template_dir], [{"text": "Icon", "target": "icon.png"}], "my_thing")
3333

3434
assert "icon.png" in str(exc_info.value)
3535
assert "binary file" in str(exc_info.value)
36+
assert "my_thing" in str(exc_info.value)
3637

3738

3839
def test_load_linked_resources_missing_file_raises_file_not_found(template_dir):
3940
with pytest.raises(FileNotFoundError):
40-
load_linked_resources([template_dir], [{"text": "Missing", "target": "missing.md"}])
41+
load_linked_resources([template_dir], [{"text": "Missing", "target": "missing.md"}], "my_thing")

0 commit comments

Comments
 (0)