Skip to main content

jinja-try-catch

A Jinja2 extension providing exception handling within templates.

{%- try -%}
  {{ 1 / 0 }}
{%- catch -%}
  Can't divide by zero!
{%- endtry -%}
Can't divide by zero!

This extension works in both sync and async environments, as well as the native variants, too.

Quickstart

pip install jinja-try-catch

And add the TryCatchExtension to your Environment extensions list:

import jinja2
from jinja_try_catch import TryCatchExtension

jinja_env = jinja2.Environment(extensions=[TryCatchExtension])

Usage

Suppressing errors

Simply omit the {% catch %} to silently swallow exceptions

{%- try -%}
  {{ 1 / 0 }}
{%- endtry -%}

Error messages

Define a {% catch %} to render something else if an exception is raised

{%- try -%}
  {{ 1 / 0 }}
{%- catch -%}
  There are infinite zeroes, duh
{%- endtry -%}
There are infinite zeroes, duh

Display exceptions

The raised exception is exposed within the {% catch %} through the {{ exception }} variable

{%- try -%}
  {{ 1 / 0 }}
{%- catch -%}
  Uh-oh, an error occurred:
  {{ exception.__class__.__name__ }}: {{ exception }}
{%- endtry -%}
Uh-oh, an error occurred:
  ZeroDivisionError: division by zero

Metadata

Release files for jinja-try-catch 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja-try-catch 0.1.1
File Size Uploaded
jinja_try_catch-0.1.1.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jinja-try-catch 0.1.1
File Interpreter ABI Platform
jinja_try_catch-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.4 kB

Release files / jinja_try_catch-0.1.1.tar.gz

Download URL jinja_try_catch-0.1.1.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
593a3ea0ef13c4db01225cfd66bd8171f0153e89ab3d8fdba496f0f80110ca1d
BLAKE2b-256 checksum
How to use checksums
ad28ccb1e367663b157c90d85a4a2544890a8fd998f160d5bb1cfb6b0a921d93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Linux/5.13.0-52-generic

Release files / jinja_try_catch-0.1.1-py3-none-any.whl

Download URL jinja_try_catch-0.1.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d653a5fbbcddbb454770346af1a314042a9051ce910bce57c8a4cd7f33de0589
BLAKE2b-256 checksum
How to use checksums
aa74ba11031f595291bab0d1fb92dd28358230cb37503ba91076602f965a1478
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Linux/5.13.0-52-generic

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page