Skip to content

Commit

Permalink
Fix #11012: Catch DbtRuntimeError for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
aranke committed Nov 21, 2024
1 parent a42303c commit 6a0c71a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/dbt/task/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ def after_run(self, adapter, results) -> None:
try:
with adapter.connection_named("master"):
self.safe_run_hooks(adapter, RunHookType.End, extras)
except (KeyboardInterrupt, SystemExit):
except (KeyboardInterrupt, SystemExit, DbtRuntimeError):
run_result = self.get_result(
results=self.node_results,
elapsed_time=time.time() - self.started_at,
Expand Down
36 changes: 35 additions & 1 deletion tests/functional/adapter/hooks/test_on_run_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from dbt.artifacts.schemas.results import RunStatus
from dbt.contracts.graph.nodes import HookNode
from dbt.tests.util import get_artifact, run_dbt_and_capture
from dbt.tests.util import get_artifact, run_dbt, run_dbt_and_capture
from dbt_common.exceptions import CompilationError


class Test__StartHookFail__FlagIsNone__ModelFail:
Expand Down Expand Up @@ -242,3 +243,36 @@ def test_results_in_context_hook_fail(self, project):
assert "Thread ID: main" in log_output
assert results[0].thread_id == "main"
assert "Num Results in context: 2" in log_output # failed hook and model


class Test__HookCompilationError:
@pytest.fixture(scope="class")
def models(self):
return {"my_model.sql": "select 1 as id"}

@pytest.fixture(scope="class")
def macros(self):
return {
"rce.sql": """
{% macro rce(relation) %}
{% if execute %}
{{ exceptions.raise_compiler_error("Always raise a compiler error in execute") }}
{% endif %}
{% endmacro %}
"""
}

@pytest.fixture(scope="class")
def project_config_update(self):
return {
"on-run-end": ["{{ rce() }}"],
}

def test_results(self, project):
with pytest.raises(CompilationError, match="Always raise a compiler error in execute"):
run_dbt(["run"], expect_pass=False)

run_results = get_artifact(project.project_root, "target", "run_results.json")
assert [(result["unique_id"], result["status"]) for result in run_results["results"]] == [
("model.test.my_model", RunStatus.Success)
]

0 comments on commit 6a0c71a

Please sign in to comment.