@@ -68,10 +68,19 @@ def __init__(self, template: str, start_position: int, end_position: int):
68
68
self .content = template [start_position :end_position ]
69
69
70
70
71
+ class TemplateNotFoundError (OSError ):
72
+ """Raised when a template file is not found."""
73
+
74
+ def __init__ (self , path : str ):
75
+ """Specified template file that was not found."""
76
+ super ().__init__ (f"Template file not found: { path } " )
77
+
78
+
71
79
class TemplateSyntaxError (SyntaxError ):
72
80
"""Raised when a syntax error is encountered in a template."""
73
81
74
82
def __init__ (self , message : str , token : Token ):
83
+ """Provided token is not a valid template syntax at the specified position."""
75
84
super ().__init__ (f"{ message } \n \n " + self ._underline_token_in_template (token ))
76
85
77
86
@staticmethod
@@ -302,7 +311,7 @@ def _resolve_includes(template: str):
302
311
# TODO: Restrict include to specific directory
303
312
304
313
if not _exists_and_is_file (template_path ):
305
- raise OSError ( f"Template file not found: { template_path } " )
314
+ raise TemplateNotFoundError ( template_path )
306
315
307
316
# Replace the include with the template content
308
317
with open (template_path , "rt" , encoding = "utf-8" ) as template_file :
@@ -323,7 +332,7 @@ def _resolve_includes_blocks_and_extends(template: str):
323
332
extended_template_path = extends_match .group (0 )[12 :- 4 ]
324
333
325
334
if not _exists_and_is_file (extended_template_path ):
326
- raise OSError ( f"Template file not found: { extended_template_path } " )
335
+ raise TemplateNotFoundError ( extended_template_path )
327
336
328
337
# Check for circular extends
329
338
if extended_template_path in extended_templates :
@@ -833,7 +842,7 @@ def __init__(self, template_path: str, *, language: str = Language.HTML) -> None
833
842
"""
834
843
835
844
if not _exists_and_is_file (template_path ):
836
- raise OSError ( f"Template file not found: { template_path } " )
845
+ raise TemplateNotFoundError ( template_path )
837
846
838
847
with open (template_path , "rt" , encoding = "utf-8" ) as template_file :
839
848
template_string = template_file .read ()
0 commit comments