-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEAT-488]😎 Session exceptions (#670)
* refactor: added shell for new exception classà * refactor: setup decorator * refactor; using neat session
- Loading branch information
Showing
4 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import functools | ||
from collections.abc import Callable | ||
from typing import Any | ||
|
||
try: | ||
from rich import print | ||
|
||
_PREFIX = "[bold red][ERROR][/bold red]" | ||
except ImportError: | ||
_PREFIX = "[ERROR]" | ||
|
||
|
||
class NeatSessionError(Exception): | ||
"""Base class for all exceptions raised by the NeatSession class.""" | ||
|
||
... | ||
|
||
|
||
def _intercept_session_exceptions(func: Callable): | ||
@functools.wraps(func) | ||
def wrapper(*args: Any, **kwargs: Any): | ||
try: | ||
return func(*args, **kwargs) | ||
except NeatSessionError as e: | ||
action = func.__name__ | ||
print(f"{_PREFIX} Cannot {action}: {e}") | ||
|
||
return wrapper | ||
|
||
|
||
def intercept_session_exceptions(cls: type): | ||
to_check = [cls] | ||
while to_check: | ||
cls = to_check.pop() | ||
for attr_name in dir(cls): | ||
if not attr_name.startswith("_"): | ||
attr = getattr(cls, attr_name) | ||
if callable(attr): | ||
setattr(cls, attr_name, _intercept_session_exceptions(attr)) | ||
elif isinstance(attr, type): | ||
to_check.append(attr) | ||
return cls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters