Skip to content

Commit 9dd2c81

Browse files
Log any potential encoding errors in template scanning (#215)
* Log any potential encoding errors in template scanning - Added logging for template processing errors - Improved error handling in find_components_in_template to catch UnicodeDecodeError - Added tests for proper handling of binary/invalid files during template scanning - Updated IncludeNode visitor to log errors when processing included templates * change to the tilde syntax? * okay...
1 parent fb976dd commit 9dd2c81

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
3434
### Fixed
3535

3636
- Fixed bug where assets from components with the same name in different directories weren't being properly collected
37+
- Fixed template scanning on some platforms by improving error handling for invalid template files
3738

3839
### Removed
3940

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,4 @@ required-imports = ["from __future__ import annotations"]
254254
keep-runtime-typing = true
255255

256256
[tool.uv]
257-
required-version = "==0.6.*"
257+
required-version = "<0.7"

src/django_bird/templates.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import logging
34
import multiprocessing
45
from collections.abc import Callable
56
from collections.abc import Generator
@@ -28,6 +29,8 @@
2829
from .utils import get_files_from_dirs
2930
from .utils import unique_ordered
3031

32+
logger = logging.getLogger(__name__)
33+
3134

3235
def get_template_names(name: str) -> list[str]:
3336
"""
@@ -155,8 +158,11 @@ def find_components_in_template(template_path: str | Path) -> set[str]:
155158
with context.bind_template(template):
156159
visitor.visit(template, context)
157160
return visitor.components
158-
except (TemplateDoesNotExist, TemplateSyntaxError):
159-
# If we can't load the template, return an empty set
161+
except (TemplateDoesNotExist, TemplateSyntaxError, UnicodeDecodeError) as e:
162+
# If we can't load or process the template for any reason, log the exception and return an empty set
163+
logger.debug(
164+
f"Could not process template {template_name!r}: {e.__class__.__name__}: {e}"
165+
)
160166
return set()
161167

162168

@@ -209,6 +215,8 @@ def visit_IncludeNode(self, node: IncludeNode, context: Context) -> None:
209215
for template_name in included_templates:
210216
included_template = self.engine.get_template(template_name)
211217
self.visit(included_template, context)
212-
except Exception:
213-
pass
218+
except Exception as e:
219+
logger.debug(
220+
f"Error processing included template in NodeVisitor: {e.__class__.__name__}: {e}"
221+
)
214222
self.generic_visit(node, context)

tests/test_templates.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import pytest
44
from django.test import override_settings
55

6+
from django_bird.templates import find_components_in_template
7+
from django_bird.templates import gather_bird_tag_template_usage
68
from django_bird.templates import get_template_names
79

810

@@ -68,3 +70,27 @@ def test_get_template_names_duplicates():
6870

6971
for _, count in template_counts.items():
7072
assert count == 1
73+
74+
75+
def test_find_components_handles_errors():
76+
result = find_components_in_template("non_existent_template.html")
77+
assert result == set()
78+
79+
80+
def test_find_components_handles_encoding_errors(templates_dir):
81+
binary_file = templates_dir / "binary_file.html"
82+
with open(binary_file, "wb") as f:
83+
f.write(b"\x80\x81\x82invalid binary content\xfe\xff")
84+
85+
valid_file = templates_dir / "valid_file.html"
86+
valid_file.write_text("""
87+
<html>
88+
<body>
89+
{% bird button %}Button{% endbird %}
90+
</body>
91+
</html>
92+
""")
93+
94+
results = list(gather_bird_tag_template_usage())
95+
96+
assert all(str(valid_file) in str(path) for path, _ in results)

0 commit comments

Comments
 (0)