Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make error strings better #42

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Under the Hood-20240906-184939.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Under the Hood
body: Better error messages
time: 2024-09-06T18:49:39.208985+02:00
2 changes: 1 addition & 1 deletion dbtsl/backoff.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ def iter_ms(self) -> Iterator[int]:
curr_ms = int(time.time() * 1000)
elapsed_ms = curr_ms - start_ms
if elapsed_ms > self.timeout_ms:
raise TimeoutError()
raise TimeoutError(timeout_ms=self.timeout_ms)

yield min(int(self.base_interval_ms * self.exp_factor**i), self.max_interval_ms)
21 changes: 21 additions & 0 deletions dbtsl/error.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
class SemanticLayerError(RuntimeError):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these exceptions take in an error message?

"""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__

def __repr__(self) -> str:
"""RuntimeError doesn't stringify itself by default, so we need to manually add this."""
return str(self)


class TimeoutError(SemanticLayerError):
"""Raise whenever a request times out."""

def __init__(self, *args, timeout_ms: int, **kwargs) -> None:
"""Initialize the timeout error.

Args:
timeout_ms: The maximum time limit that got exceeded, in milliseconds
*args: any other exception args
**kwargs: any other exception kwargs
"""
self.timeout_ms = timeout_ms

def __str__(self) -> str: # noqa: D105
return f"{self.__class__.__name__}(timeout_ms={self.timeout_ms})"


class QueryFailedError(SemanticLayerError):
"""Raise whenever a query has failed."""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from dbtsl.error import SemanticLayerError, TimeoutError


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


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


def test_timeout_error_str() -> None:
assert str(TimeoutError(timeout_ms=1000)) == "TimeoutError(timeout_ms=1000)"