Skip to content

Commit

Permalink
Check enabled setting. Rename fallback to default.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill committed Nov 22, 2024
1 parent b8109bf commit 54a1a31
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
3 changes: 3 additions & 0 deletions example/www/templates/www/components/error-fallback.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
This is an error fallback template.
</p>
16 changes: 10 additions & 6 deletions example/www/templates/www/error-boundaries.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
<dj-extends parent='www/base.html' />

<dj-block name='content' error-boundary>
<dj-include src="www/components/include-bad1.html" />
<dj-include src="www/components/include-bad.html" />
</dj-block>

<dj-block name='extra-content'>
<dj-error-boundary default='www/components/error-fallback.html'>
<dj-include src="www/components/include-bad.html" />
</dj-error-boundary>

<dj-error-boundary default='This is an error fallback string'>
<dj-include src="www/components/include-bad.html" />
</dj-error-boundary>

<dj-error-boundary>
<dj-include src="www/components/include-bad.html" />
</dj-error-boundary>

<dj-error-boundary>
<dj-include src="www/components/include-bad1.html" />
<dj-include src="missing.html" />
</dj-error-boundary>

<dj-error-boundary>
<dj-include src="www/components/include.html" />
</dj-error-boundary>

<dj-include src="www/components/include.html" />

<dj-include src="www/components/include.html" />

<dj-include src="www/components/include.html" />
</dj-block>
27 changes: 14 additions & 13 deletions src/dj_angles/regex_replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ def check_for_missing_start_tag(raise_for_missing_start_tag, tag, tag_queue):
def get_replacements_for_error_boundaries(tag_regex, origin, tag, replacements):
matches_to_skip = 0

if tag.is_error_boundary and tag.inner_html:
matches_to_skip = len(re.findall(tag_regex, tag.inner_html))
if get_setting(key_path="error_boundaries", setting_name="enabled", default=True) is True:
if tag.is_error_boundary and tag.inner_html:
matches_to_skip = len(re.findall(tag_regex, tag.inner_html))

try:
parsed_inner_html = replace_django_template_tags(tag.inner_html)
try:
parsed_inner_html = replace_django_template_tags(tag.inner_html)

# Parse the inner HTML and create a template
inner_html_template = Template(parsed_inner_html, origin=origin)
# Parse the inner HTML and create a template
inner_html_template = Template(parsed_inner_html, origin=origin)

# It would be nice to pass in the template context here, but cannot
# find access to it with this process, so it is empty
inner_html_template.render(context=Context())
# It would be nice to pass in the template context here, but cannot
# find access to it with this process, so it is empty
inner_html_template.render(context=Context())

replacements.append((tag.inner_html, parsed_inner_html))
except (TemplateDoesNotExist, TemplateSyntaxError) as e:
error_html = tag.get_error_html(e)
replacements.append((tag.inner_html, parsed_inner_html))
except (TemplateDoesNotExist, TemplateSyntaxError) as e:
error_html = tag.get_error_html(e)

replacements.append((tag.inner_html, error_html))
replacements.append((tag.inner_html, error_html))

return matches_to_skip

Expand Down
10 changes: 4 additions & 6 deletions src/dj_angles/tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import TYPE_CHECKING, Optional

from django.conf import settings
from django.template import Origin, TemplateDoesNotExist, TemplateSyntaxError
from django.utils.html import escape
from minestrone import Element

from dj_angles.attributes import Attributes
Expand All @@ -19,7 +17,7 @@


SHADOW_ATTRIBUTE_KEY = "shadow"
FALLBACK_ATTRIBUTE_KEY = "fallback"
ERROR_FALLBACK_ATTRIBUTE_KEY = "default"

ERROR_BOUNDARY_ATTRIBUTE_KEY = "error-boundary"
ERROR_BOUNDARY_TAG_NAMES = ["block"]
Expand Down Expand Up @@ -91,9 +89,9 @@ def __init__(
self.is_error_boundary = True
self.attributes.remove(ERROR_BOUNDARY_ATTRIBUTE_KEY)

if self.attributes.has(FALLBACK_ATTRIBUTE_KEY):
self.error_fallback = dequotify(self.attributes.get(FALLBACK_ATTRIBUTE_KEY).value)
self.attributes.remove(FALLBACK_ATTRIBUTE_KEY)
if self.is_error_boundary and self.attributes.has(ERROR_FALLBACK_ATTRIBUTE_KEY):
self.error_fallback = dequotify(self.attributes.get(ERROR_FALLBACK_ATTRIBUTE_KEY).value)
self.attributes.remove(ERROR_FALLBACK_ATTRIBUTE_KEY)

if get_setting("lower_case_tag", default=False) is True:
self.tag_name = self.tag_name.lower()
Expand Down

0 comments on commit 54a1a31

Please sign in to comment.