Skip to content

Commit 500fb7d

Browse files
authored
Display dbt parse errors (#126)
When running dbt-score, if dbt parse fails, the error message does not reflect the root cause of the failure. This PR attaches the root cause to the DbtParseException object and prints it to the console. Here’s an example of how it looks: ```shell $ dbt-score lint -p ERROR:dbt_score.cli:dbt failed to parse project. dbt parse failed. Root cause: Compilation Error Model 'model.edge.example_incremental_model' (models/example_incremental_model/example_incremental_model.sql) depends on a node named 'example_overwrite_not_exists_model' which was not found ```
1 parent 57c3545 commit 500fb7d

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to
88

99
## [Unreleased]
1010

11+
- Display the parse error message when `dbt parse` fails.
12+
1113
## [0.13.1] - 2025-07-29
1214

1315
- Fix filters being applied to wrong evaluables (#124)

src/dbt_score/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ def lint( # noqa: PLR0912, PLR0913, C901
165165
)
166166
ctx.exit(2)
167167

168-
except DbtParseException:
169-
logger.error("dbt failed to parse project. Run `dbt parse` to investigate.")
168+
except DbtParseException as exc:
169+
logger.error(exc)
170170
ctx.exit(2)
171171

172172
except Exception:

src/dbt_score/dbt_utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ class DbtNotInstalledException(Exception):
2121
class DbtParseException(Exception):
2222
"""Raised when dbt parse fails."""
2323

24+
def __init__(self, root_cause: Exception | None = None):
25+
"""Initialize the exception."""
26+
super().__init__()
27+
self.root_cause = root_cause
28+
29+
def __str__(self) -> str:
30+
"""Return a string representation of the exception."""
31+
if self.root_cause:
32+
return f"dbt parse failed.\n\n{self.root_cause!s}"
33+
34+
return (
35+
"dbt parse failed. Root cause not found. Please run `dbt parse` manually."
36+
)
37+
2438

2539
class DbtLsException(Exception):
2640
"""Raised when dbt ls fails."""
@@ -61,7 +75,7 @@ def dbt_parse() -> "dbtRunnerResult":
6175
result: "dbtRunnerResult" = dbtRunner().invoke(["parse"])
6276

6377
if not result.success:
64-
raise DbtParseException("dbt parse failed.") from result.exception
78+
raise DbtParseException(root_cause=result.exception)
6579

6680
return result
6781

tests/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def test_lint_dbt_parse_exception(caplog):
5252
runner = CliRunner()
5353

5454
with patch("dbt_score.cli.dbt_parse") as mock_dbt_parse:
55-
mock_dbt_parse.side_effect = DbtParseException("parsing error")
55+
mock_dbt_parse.side_effect = DbtParseException()
5656
result = runner.invoke(lint, ["-p"], catch_exceptions=False)
5757
assert result.exit_code == 2
58-
assert "dbt failed to parse project" in caplog.text
58+
assert "dbt parse failed." in caplog.text
5959

6060

6161
def test_lint_dbt_not_installed(caplog, manifest_path):

0 commit comments

Comments
 (0)