-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make EmitLoop error message clearer (#232)
* refactor: adjust traceback of callback * merge * style(pre-commit.ci): auto fixes [...] * slot repr * fix: fix type * coverage * style(pre-commit.ci): auto fixes [...] --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b900ff2
commit 5602780
Showing
5 changed files
with
90 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,47 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Callable | ||
|
||
from ._weak_callback import WeakCallback | ||
|
||
if TYPE_CHECKING: | ||
from ._signal import SignalInstance | ||
|
||
MSG = """ | ||
While emitting signal {sig!r}, an error occurred in callback {cb!r}. | ||
The args passed to the callback were: {args!r} | ||
This is not a bug in psygnal. See {err!r} above for details. | ||
""" | ||
|
||
|
||
class EmitLoopError(Exception): | ||
"""Error type raised when an exception occurs during a callback.""" | ||
|
||
def __init__(self, slot_repr: str, args: tuple, exc: BaseException) -> None: | ||
self.slot_repr = slot_repr | ||
def __init__( | ||
self, | ||
cb: WeakCallback | Callable, | ||
args: tuple, | ||
exc: BaseException, | ||
signal: SignalInstance | None = None, | ||
) -> None: | ||
self.exc = exc | ||
self.args = args | ||
self.__cause__ = exc # mypyc doesn't set this, but uncompiled code would | ||
if signal is None: | ||
sig_name = "" | ||
else: | ||
inst_class = signal.instance.__class__ | ||
mod = getattr(inst_class, "__module__", "") | ||
sig_name = f"{mod}.{inst_class.__qualname__}.{signal.name}" | ||
if isinstance(cb, WeakCallback): | ||
cb_name = cb.slot_repr() | ||
else: | ||
cb_name = getattr(cb, "__qualname__", repr(cb)) | ||
super().__init__( | ||
f"calling {self.slot_repr} with args={args!r} caused " | ||
f"{type(exc).__name__}: {exc}." | ||
MSG.format( | ||
sig=sig_name, | ||
cb=cb_name, | ||
args=args, | ||
err=exc.__class__.__name__, | ||
) | ||
) |
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