Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch jinja typeerrors during render phase #207

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading