Skip to content

Commit

Permalink
fix: catch jinja typeerrors on render phase
Browse files Browse the repository at this point in the history
  • Loading branch information
devmessias committed Oct 16, 2024
1 parent 1bad6b7 commit 74b437e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dbt_common/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
MaterializationArgError,
JinjaRenderingError,
UndefinedCompilationError,
DbtRuntimeTypeError
)
from dbt_common.exceptions.macros import MacroReturn, UndefinedMacroError, CaughtMacroError

Expand Down Expand Up @@ -125,6 +126,14 @@ def _compile(self, source: str, filename: str) -> CodeType:
return super()._compile(source, filename) # type: ignore


class RenderCallJinjaTypeError(jinja2.TemplateError):
def __init__(
self,
message: str,
) -> None:
super().__init__(message)


class MacroFuzzTemplate(jinja2.nativetypes.NativeTemplate):
environment_class = MacroFuzzEnvironment # type: ignore

Expand Down Expand Up @@ -159,6 +168,8 @@ def render(self, *args: Any, **kwargs: Any) -> Any:
return self.environment_class.concat( # type: ignore
self.root_render_func(ctx) # type: ignore
)
except TypeError as e:
raise RenderCallJinjaTypeError(str(e)) from e
except Exception:
return self.environment.handle_exception()

Expand Down Expand Up @@ -531,6 +542,8 @@ def catch_jinja(node: Optional[_NodeProtocol] = None) -> Iterator[None]:
raise CompilationError(str(e), node) from e
except jinja2.exceptions.UndefinedError as e:
raise UndefinedMacroError(str(e), node) from e
except RenderCallJinjaTypeError as e:
raise DbtRuntimeTypeError(str(e), node) from e
except CompilationError as exc:
exc.add_node(node)
raise
Expand Down
8 changes: 8 additions & 0 deletions dbt_common/exceptions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,11 @@ def __str__(self, prefix: str = "! ") -> str:
if len(self.cmd) == 0:
return f"{self.msg}: No arguments given"
return f'{self.msg}: "{self.cmd[0]}"'


class DbtRuntimeTypeError(DbtRuntimeError):
MESSAGE = "Type Error"

@property
def type(self):
return "Type"

0 comments on commit 74b437e

Please sign in to comment.