jinja-try-catch
raw JSON → 0.1.1 verified Sat May 09 auth: no python
Jinja2 extension adding {% try %} {% catch %} exception handling blocks. Version 0.1.1, requires Python >=3.7. Low release cadence, maintained as needed.
pip install jinja-try-catch Common errors
error jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'try'. ↓
cause Extension not loaded/enabled in the Jinja2 environment.
fix
Add
TryExtension to extensions list when creating Environment: Environment(extensions=[TryExtension()]) error NameError: name 'error' is not defined ↓
cause Using `{{ error }}` outside the {% catch %} block or before a try block.
fix
Only use
{{ error }} within the {% catch %} block. Warnings
gotcha `error` variable is only available inside the {% catch %} block. Referencing it outside will cause an undefined variable error. ↓
fix Only access `{{ error }}` within the {% catch %} block.
deprecated Extension registered via class name. Future versions may change the API for enabling extensions; check changelog. ↓
fix Continue using `TryExtension` class; no current deprecation but be aware of potential API changes.
gotcha Catch block catches all exceptions; no filtering by exception type is supported. ↓
fix Use conditional logic inside catch block if needed: `{% catch %}{% if 'specific' in error %}...{% endif %}{% endtry %}`.
Imports
- TryExtension wrong
from jinja_try_catch.try_catch import TryExtensioncorrectfrom jinja_try_catch import TryExtension
Quickstart
from jinja2 import Environment
from jinja_try_catch import TryExtension
env = Environment(extensions=[TryExtension()])
template = env.from_string("""
{% try %}
{{ 1/0 }}
{% catch %}
Caught error: {{ error }}
{% endtry %}
""")
print(template.render())
# Output: Caught error: division by zero