forked from 3liz/qgis_plugin_tools
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow custom logger name for slot exception handler
- Loading branch information
Showing
3 changed files
with
41 additions
and
20 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 |
---|---|---|
|
@@ -3,35 +3,48 @@ | |
__email__ = "[email protected]" | ||
__revision__ = "$Format:%H$" | ||
|
||
from typing import Any, Callable | ||
from typing import Any, Callable, Optional | ||
|
||
from .exceptions import QgsPluginException | ||
from .i18n import tr | ||
from .messages import MsgBar | ||
from .messages import MessageBarLogger | ||
from .tasks import FunctionTask | ||
|
||
|
||
def log_if_fails(fn: Callable) -> Callable: | ||
def log_if_fails( | ||
fn: Optional[Callable] = None, /, *, logger_name: str = __name__ | ||
) -> Callable: | ||
""" | ||
Use this as a decorator with functions and methods that | ||
might throw uncaught exceptions. | ||
""" | ||
from functools import wraps | ||
|
||
@wraps(fn) | ||
def wrapper(*args: Any, **kwargs: Any) -> None: # noqa: ANN001 | ||
try: | ||
# Qt injects False into some signals | ||
if args[1:] != (False,): | ||
fn(*args, **kwargs) | ||
else: | ||
fn(*args[:-1], **kwargs) | ||
except QgsPluginException as e: | ||
MsgBar.exception(e, **e.bar_msg, stack_info=True) | ||
except Exception as e: | ||
MsgBar.exception(tr("Unhandled exception occurred"), e, stack_info=True) | ||
|
||
return wrapper | ||
# caller is at depth 3 (MessageBarLogger log call, this function, actual call) | ||
message_bar = MessageBarLogger(logger_name, stack_level=3) | ||
|
||
def decorator(fn: Callable) -> Callable: | ||
@wraps(fn) | ||
def wrapper(*args: Any, **kwargs: Any) -> None: # noqa: ANN001 | ||
try: | ||
# Qt injects False into some signals | ||
if args[1:] != (False,): | ||
fn(*args, **kwargs) | ||
else: | ||
fn(*args[:-1], **kwargs) | ||
except QgsPluginException as e: | ||
message_bar.exception(e, **e.bar_msg, stack_info=True) | ||
except Exception as e: | ||
message_bar.exception( | ||
tr("Unhandled exception occurred"), e, stack_info=True | ||
) | ||
|
||
return wrapper | ||
|
||
if fn is None: | ||
return decorator | ||
|
||
return decorator(fn) | ||
|
||
|
||
def taskify(fn: Callable) -> Callable: | ||
|
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