Skip to content

Commit e396c8c

Browse files
authored
Chore: improve error message when no MODEL block (#4588)
1 parent 194fd92 commit e396c8c

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

sqlmesh/core/loader.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,12 @@ def _track_file(self, path: Path) -> None:
452452
self._path_mtimes[path] = path.stat().st_mtime
453453

454454
def _failed_to_load_model_error(self, path: Path, error: t.Union[str, Exception]) -> str:
455-
base_message = f"Failed to load model definition at '{path}':"
455+
base_message = f"Failed to load model from file '{path}':"
456456
if isinstance(error, ValidationError):
457457
return validation_error_message(error, base_message)
458-
return f"{base_message}\n {error}"
458+
# indent all lines of error message
459+
error_message = str(error).replace("\n", "\n ")
460+
return f"{base_message}\n\n {error_message}"
459461

460462

461463
class SqlMeshLoader(Loader):

sqlmesh/core/model/definition.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,18 +2036,25 @@ def load_sql_based_model(
20362036
variables: The variables to pass to the model.
20372037
kwargs: Additional kwargs to pass to the loader.
20382038
"""
2039+
missing_model_msg = f"""Please add a MODEL block at the top of the file. Example:
2040+
2041+
MODEL (
2042+
name sqlmesh_example.full_model, --model name
2043+
kind FULL, --materialization
2044+
cron '@daily', --schedule
2045+
);
2046+
2047+
Learn more at https://sqlmesh.readthedocs.io/en/stable/concepts/models/overview
2048+
"""
2049+
20392050
if not expressions:
2040-
raise_config_error("Incomplete model definition, missing MODEL statement", path)
2051+
raise_config_error(missing_model_msg)
20412052

20422053
dialect = dialect or ""
20432054
meta = expressions[0]
20442055
if not isinstance(meta, d.Model):
20452056
if not infer_names:
2046-
raise_config_error(
2047-
"The MODEL statement is required as the first statement in the definition, "
2048-
"unless model name inference is enabled.",
2049-
path,
2050-
)
2057+
raise_config_error(missing_model_msg)
20512058
meta = d.Model(expressions=[]) # Dummy meta node
20522059
expressions.insert(0, meta)
20532060

tests/core/test_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def execute(
159159

160160
with pytest.raises(
161161
ConfigError,
162-
match=r"Failed to load model definition at '.*'.\n Duplicate name: 'test_schema.test_model'.",
162+
match=r"Failed to load model from file '.*'.\n\n Duplicate name: 'test_schema.test_model'.",
163163
):
164164
Context(paths=tmp_path, config=config)
165165

tests/core/test_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def test_no_model_statement(tmp_path: Path):
576576
expressions = d.parse("SELECT 1 AS x")
577577
with pytest.raises(
578578
ConfigError,
579-
match="The MODEL statement is required as the first statement in the definition, unless model name inference is enabled. at '.'",
579+
match="Please add a MODEL block at the top of the file. Example:",
580580
):
581581
load_sql_based_model(expressions)
582582

@@ -613,7 +613,7 @@ def test_unordered_model_statements():
613613

614614
with pytest.raises(ConfigError) as ex:
615615
load_sql_based_model(expressions)
616-
assert "MODEL statement is required" in str(ex.value)
616+
assert "Please add a MODEL block at the top of the file. Example:" in str(ex.value)
617617

618618

619619
def test_no_query():

0 commit comments

Comments
 (0)