Skip to content

Commit

Permalink
refactor: fix error messages
Browse files Browse the repository at this point in the history
This commit adds explicit logging of `self.args` in `SemanticLayerError`
so that users get a proper display of the underlying error when they
`str(e)` or `repr(e)`.

It also adds `raise from` in `_handle_error` to add more context to the
exceptions raised via ADBC.
  • Loading branch information
serramatutu committed Oct 8, 2024
1 parent 22c919b commit dc48b1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Under the Hood-20241008-160709.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Under the Hood
body: Improve error display
time: 2024-10-08T16:07:09.766189+02:00
6 changes: 3 additions & 3 deletions dbtsl/api/adbc/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def _get_connection_context_manager(self) -> AbstractContextManager[Connection]:
def _handle_error(self, err: Exception) -> None:
if isinstance(err, ProgrammingError):
if err.status_code in (AdbcStatusCode.UNAUTHENTICATED, AdbcStatusCode.UNAUTHORIZED):
raise AuthError(err.args)
raise AuthError(err.args) from err

if err.status_code == AdbcStatusCode.INVALID_ARGUMENT:
raise QueryFailedError(err.args)
raise QueryFailedError(err.args) from err

# TODO: timeouts are not implemented for ADBC
# See: https://arrow.apache.org/adbc/current/driver/flight_sql.html#timeouts
if err.status_code == AdbcStatusCode.TIMEOUT:
raise TimeoutError()
raise TimeoutError() from err

raise err

Expand Down
6 changes: 5 additions & 1 deletion dbtsl/error.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import json


class SemanticLayerError(RuntimeError):
"""Any errors related to the Semantic Layer inherit from this."""

def __str__(self) -> str:
"""RuntimeError doesn't stringify itself by default, so we need to manually add this."""
return self.__class__.__name__
args_str = "" if len(self.args) == 0 else ", ".join(json.dumps(a) for a in self.args)
return f"{self.__class__.__name__}({args_str})"

def __repr__(self) -> str:
"""RuntimeError doesn't stringify itself by default, so we need to manually add this."""
Expand Down
12 changes: 8 additions & 4 deletions tests/test_error.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from dbtsl.error import SemanticLayerError, TimeoutError


def test_error_str() -> None:
assert str(SemanticLayerError()) == "SemanticLayerError"
def test_error_str_calls_repr() -> None:
assert str(SemanticLayerError()) == repr(SemanticLayerError())


def test_error_repr() -> None:
assert repr(SemanticLayerError()) == "SemanticLayerError"
def test_error_repr_no_args() -> None:
assert repr(SemanticLayerError()) == "SemanticLayerError()"


def test_error_repr_with_args() -> None:
assert repr(SemanticLayerError(1, 2, "a")) == 'SemanticLayerError(1, 2, "a")'


def test_timeout_error_str() -> None:
Expand Down

0 comments on commit dc48b1d

Please sign in to comment.