Skip to content

Commit

Permalink
clean up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Jul 19, 2024
1 parent 42c2b0b commit f247b55
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
16 changes: 6 additions & 10 deletions core/dbt/clients/jinja_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ def statically_parse_ref(input: str) -> RefArgs:
try:
statically_parsed = py_extract_from_source(f"{{{{ {input} }}}}")
except ExtractionError:
# TODO: more precise error
raise ParsingError(f"Invalid jinja expression: {input}.")
raise ParsingError(f"Invalid jinja expression: {input}")

Check warning on line 176 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L173-L176

Added lines #L173 - L176 were not covered by tests

if not statically_parsed["refs"]:
# TODO: more precise error class
raise ParsingError("not a ref")
if not statically_parsed.get("refs"):
raise ParsingError(f"Invalid ref expression: {input}")

Check warning on line 179 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L178-L179

Added lines #L178 - L179 were not covered by tests

ref = list(statically_parsed["refs"])[0]
return RefArgs(package=ref.get("package"), name=ref.get("name"), version=ref.get("version"))

Check warning on line 182 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L181-L182

Added lines #L181 - L182 were not covered by tests
Expand All @@ -198,12 +196,10 @@ def statically_parse_source(input: str) -> Tuple[str, str]:
try:
statically_parsed = py_extract_from_source(f"{{{{ {input} }}}}")
except ExtractionError:
# TODO: more precise error
raise ParsingError(f"Invalid jinja expression: {input}.")
raise ParsingError(f"Invalid jinja expression: {input}")

Check warning on line 199 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L196-L199

Added lines #L196 - L199 were not covered by tests

if not statically_parsed["sources"]:
# TODO: more precise error class
raise ParsingError("not a ref")
if not statically_parsed.get("sources"):
raise ParsingError(f"Invalid source expression: {input}")

Check warning on line 202 in core/dbt/clients/jinja_static.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/clients/jinja_static.py#L201-L202

Added lines #L201 - L202 were not covered by tests

source = list(statically_parsed["sources"])[0]
source_name, source_table_name = source
Expand Down
8 changes: 7 additions & 1 deletion core/dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
from dbt.exceptions import (
DbtInternalError,
DbtRuntimeError,
ForeignKeyConstraintToSyntaxError,
GraphDependencyNotFoundError,
ParsingError,
)
from dbt.flags import get_flags
from dbt.graph import Graph
Expand Down Expand Up @@ -465,7 +467,11 @@ def _compile_code(
def _compile_relation_for_foreign_key_constraint_to(
self, manifest: Manifest, node: ManifestSQLNode, to_expression: str
) -> str:
foreign_key_node = manifest.find_node_from_ref_or_source(to_expression)
try:
foreign_key_node = manifest.find_node_from_ref_or_source(to_expression)
except ParsingError:
raise ForeignKeyConstraintToSyntaxError(node, to_expression)

Check warning on line 473 in core/dbt/compilation.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/compilation.py#L470-L473

Added lines #L470 - L473 were not covered by tests

if not foreign_key_node:
raise GraphDependencyNotFoundError(node, to_expression)
adapter = get_adapter(self.config)
Expand Down
9 changes: 4 additions & 5 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
AmbiguousResourceNameRefError,
CompilationError,
DuplicateResourceNameError,
ParsingError,
)
from dbt.flags import get_flags
from dbt.mp_context import get_mp_context
Expand Down Expand Up @@ -1643,17 +1644,15 @@ def find_node_from_ref_or_source(
valid_source = True
try:
ref = statically_parse_ref(expression)
# TODO: better error handling
except Exception:
except ParsingError:
valid_ref = False
try:
source_name, source_table_name = statically_parse_source(expression)
# TODO: better error handling
except Exception:
except ParsingError:
valid_source = False

Check warning on line 1652 in core/dbt/contracts/graph/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/manifest.py#L1643-L1652

Added lines #L1643 - L1652 were not covered by tests

if not valid_ref and not valid_ref:
raise CompilationError(f"Invalid ref or source expression: {expression}")
raise ParsingError(f"Invalid ref or source syntax: {expression}.")

Check warning on line 1655 in core/dbt/contracts/graph/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/manifest.py#L1654-L1655

Added lines #L1654 - L1655 were not covered by tests

if valid_ref:
node = self.ref_lookup.find(ref.name, ref.package, ref.version, self)
Expand Down
12 changes: 12 additions & 0 deletions core/dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ def get_message(self) -> str:
return msg


class ForeignKeyConstraintToSyntaxError(CompilationError):
def __init__(self, node, expression: str) -> None:
self.expression = expression
self.node = node
super().__init__(msg=self.get_message())

Check warning on line 143 in core/dbt/exceptions.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/exceptions.py#L141-L143

Added lines #L141 - L143 were not covered by tests

def get_message(self) -> str:
msg = f"'{self.node.unique_id}' defines a foreign key constraint 'to' expression which is not valid 'ref' or 'source' syntax: {self.expression}."

Check warning on line 146 in core/dbt/exceptions.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/exceptions.py#L146

Added line #L146 was not covered by tests

return msg

Check warning on line 148 in core/dbt/exceptions.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/exceptions.py#L148

Added line #L148 was not covered by tests


# client level exceptions


Expand Down

0 comments on commit f247b55

Please sign in to comment.