From a014af8db8ed7684a52b31f4e00583886d1d9a8d Mon Sep 17 00:00:00 2001
From: Tim Neumann <git@neumann-tim.de>
Date: Tue, 26 Nov 2024 21:23:19 +0100
Subject: [PATCH 1/2] feat: Improve output in case of templating exceptions

---
 pad_initiator.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/pad_initiator.py b/pad_initiator.py
index d5bae85..6da4242 100755
--- a/pad_initiator.py
+++ b/pad_initiator.py
@@ -50,8 +50,12 @@ def templatePad(file_name: str, raw_data: str, links: Dict[str, str]) -> str:
         "links": links,
         "user_vars": user_vars
     }
-    template = Template(source=raw_data, keep_trailing_newline=True)
-    return template.render(variables_for_template)
+
+    try:
+        template = Template(source=raw_data, keep_trailing_newline=True)
+        return template.render(variables_for_template)
+    except Exception as e:
+        raise Exception(f"Failed to template for {file_name}.") from e
 
 
 def initPads(files: List[str],
-- 
GitLab


From 1d5166a82ecd3bea75121ff08aefc6d4f3ad0b4b Mon Sep 17 00:00:00 2001
From: Tim Neumann <git@neumann-tim.de>
Date: Tue, 26 Nov 2024 21:28:26 +0100
Subject: [PATCH 2/2] feat: Cause exception when using undefined variable

This is important to notice usage of links that do not exist
---
 pad_initiator.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pad_initiator.py b/pad_initiator.py
index 6da4242..b30ac15 100755
--- a/pad_initiator.py
+++ b/pad_initiator.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 from typing import List, Dict, Optional, Tuple
 
-from jinja2 import Template
+from jinja2 import Template, StrictUndefined
 
 from os import environ, path, getcwd, makedirs
 from dotenv import load_dotenv
@@ -52,7 +52,7 @@ def templatePad(file_name: str, raw_data: str, links: Dict[str, str]) -> str:
     }
 
     try:
-        template = Template(source=raw_data, keep_trailing_newline=True)
+        template = Template(source=raw_data, keep_trailing_newline=True, undefined=StrictUndefined)
         return template.render(variables_for_template)
     except Exception as e:
         raise Exception(f"Failed to template for {file_name}.") from e
-- 
GitLab