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

Raise exceptions in data registrator as warnings by default #236

Merged
merged 2 commits into from
Feb 23, 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
4 changes: 4 additions & 0 deletions cascade/base/history_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def log(self, obj: Any) -> None:
----------
obj: Any
Meta data of the object

Raises
------
MetaIOError - if log writing fails
"""

# Use serialize nac back to prevent false diffs due to
Expand Down
30 changes: 26 additions & 4 deletions cascade/meta/data_registrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import pendulum

from ..base import HistoryHandler
from ..base import HistoryHandler, MetaIOError


@dataclass
Expand Down Expand Up @@ -146,8 +146,24 @@ class DataRegistrator:
has some properties changed during the time.
"""

def __init__(self, filepath: str) -> None:
self._logger = HistoryHandler(filepath)
def __init__(self, filepath: str, raise_on_fail: bool = False) -> None:
"""
Parameters
----------
filepath : str
Path to the log file for HistoryLogger
raise_on_fail : bool, optional
Whether to raise a warning or an exception in case when
logger failed to read a file for some reason, by default False
"""
self._raise_on_fail = raise_on_fail
try:
self._logger = HistoryHandler(filepath)
except MetaIOError as e:
if self._raise_on_fail:
raise e
else:
warnings.warn(str(e))

def register(self, card: DataCard) -> None:
"""
Expand All @@ -170,4 +186,10 @@ def register(self, card: DataCard) -> None:
now = str(pendulum.now(tz="UTC"))
card.data["updated_at"] = now

self._logger.log(card.data)
try:
self._logger.log(card.data)
except MetaIOError as e:
if self._raise_on_fail:
raise e
else:
warnings.warn(str(e))