-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] Add generic error handler (#2958)
- Loading branch information
1 parent
34e5c2c
commit cc5ab0c
Showing
7 changed files
with
52 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import logging | ||
|
||
from functools import wraps | ||
from typing import Type, Optional | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class _SafeModeConfig: | ||
@staticmethod | ||
def log_exception(e: Exception, func: callable): | ||
logger.warning(f'Exception "{str(e)}" raised in function "{func.__name__}"') | ||
|
||
@staticmethod | ||
def reraise_exception(e: Exception, func: callable): | ||
raise e | ||
|
||
exception_callback = reraise_exception | ||
|
||
|
||
def enable_safe_mode(): | ||
_SafeModeConfig.exception_callback = _SafeModeConfig.log_exception | ||
|
||
|
||
def disable_safe_mode(): | ||
_SafeModeConfig.exception_callback = _SafeModeConfig.reraise_exception | ||
|
||
|
||
def set_exception_callback(callback: callable): | ||
_SafeModeConfig.exception_callback = callback | ||
|
||
|
||
def handle_exception(exc_type: Type[Exception], error_message: Optional[str] = None): | ||
def inner(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
try: | ||
return func(*args, **kwargs) | ||
except exc_type as e: # noqa | ||
if error_message is not None: | ||
logger.error(error_message) | ||
raise RuntimeError(error_message) | ||
else: # silent mode | ||
pass | ||
return wrapper | ||
return inner |
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
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 |
---|---|---|
@@ -1 +0,0 @@ | ||
from aim._ext.exception_resistant import enable_safe_mode, disable_safe_mode # noqa | ||
This file was deleted.
Oops, something went wrong.