Skip to content

Commit a4cca7a

Browse files
add error handling for rendering
1 parent 1a37dfb commit a4cca7a

2 files changed

Lines changed: 47 additions & 19 deletions

File tree

sqlmesh/core/environment.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,30 +262,35 @@ def execute_environment_statements(
262262
end: t.Optional[TimeLike] = None,
263263
execution_time: t.Optional[TimeLike] = None,
264264
) -> None:
265-
rendered_expressions = [
266-
expr
267-
for statements in environment_statements
268-
for expr in render_statements(
269-
statements=getattr(statements, runtime_stage.value),
270-
dialect=adapter.dialect,
271-
default_catalog=default_catalog,
272-
python_env=statements.python_env,
273-
jinja_macros=statements.jinja_macros,
274-
snapshots=snapshots,
275-
start=start,
276-
end=end,
277-
execution_time=execution_time,
278-
environment_naming_info=environment_naming_info,
279-
runtime_stage=runtime_stage,
280-
engine_adapter=adapter,
265+
try:
266+
rendered_expressions = [
267+
expr
268+
for statements in environment_statements
269+
for expr in render_statements(
270+
statements=getattr(statements, runtime_stage.value),
271+
dialect=adapter.dialect,
272+
default_catalog=default_catalog,
273+
python_env=statements.python_env,
274+
jinja_macros=statements.jinja_macros,
275+
snapshots=snapshots,
276+
start=start,
277+
end=end,
278+
execution_time=execution_time,
279+
environment_naming_info=environment_naming_info,
280+
runtime_stage=runtime_stage,
281+
engine_adapter=adapter,
282+
)
283+
]
284+
except Exception as e:
285+
raise SQLMeshError(
286+
f"An error occurred during rendering of the '{runtime_stage.value}' statements:\n\n{e}"
281287
)
282-
]
283288
if rendered_expressions:
284289
with adapter.transaction():
285290
for expr in rendered_expressions:
286291
try:
287292
adapter.execute(expr)
288293
except Exception as e:
289294
raise SQLMeshError(
290-
f"An error occurred during execution of the following `{runtime_stage.value}` statement:\n\n{expr}\n\n{e}"
295+
f"An error occurred during execution of the following '{runtime_stage.value}' statement:\n\n{expr}\n\n{e}"
291296
)

tests/core/test_integration.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5531,7 +5531,7 @@ def test_environment_statements_error_handling(tmp_path: Path):
55315531
ctx = Context(paths=[tmp_path], config=config)
55325532

55335533
expected_error_message = re.escape(
5534-
"""An error occurred during execution of the following `before_all` statement:
5534+
"""An error occurred during execution of the following 'before_all' statement:
55355535
55365536
CREATE TABLE identical_table (physical_schema_name TEXT)
55375537
@@ -5541,6 +5541,29 @@ def test_environment_statements_error_handling(tmp_path: Path):
55415541
with pytest.raises(SQLMeshError, match=expected_error_message):
55425542
ctx.plan(auto_apply=True, no_prompts=True)
55435543

5544+
after_all = [
5545+
"@bad_macro()",
5546+
]
5547+
5548+
config = Config(
5549+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
5550+
after_all=after_all,
5551+
)
5552+
ctx = Context(paths=[tmp_path], config=config)
5553+
5554+
expected_error_message = re.escape(
5555+
"""An error occurred during rendering of the 'after_all' statements:
5556+
5557+
Failed to resolve macros for
5558+
5559+
@bad_macro()
5560+
5561+
Macro 'bad_macro' does not exist."""
5562+
)
5563+
5564+
with pytest.raises(SQLMeshError, match=expected_error_message):
5565+
ctx.plan(auto_apply=True, no_prompts=True)
5566+
55445567

55455568
@time_machine.travel("2025-03-08 00:00:00 UTC")
55465569
def test_tz(init_and_plan_context):

0 commit comments

Comments
 (0)