1
+ from __future__ import annotations
2
+
1
3
import inspect
2
4
import logging
3
5
import sys
4
- from typing import Union , Dict , Any # noqa: F401
6
+ from typing import Any
5
7
6
- from asphalt .core import Context , qualified_name , merge_config
7
- from typeguard import check_argument_types
8
+ from asphalt .core import Context , merge_config , qualified_name
8
9
9
- from asphalt .exceptions .api import ExtrasProvider
10
+ from asphalt .exceptions .api import ExceptionReporter , ExtrasProvider
10
11
11
12
__all__ = ('report_exception' ,)
12
13
13
14
module_logger = logging .getLogger (__name__ )
14
15
15
16
16
17
def report_exception (ctx : Context , message : str , exception : BaseException = None , * ,
17
- logger : Union [ logging .Logger , str , bool ] = True ) -> None :
18
+ logger : logging .Logger | str | bool = True ) -> None :
18
19
"""
19
20
Report an exception to all exception reporters in the given context (and optionally log it too)
20
21
@@ -27,35 +28,35 @@ def report_exception(ctx: Context, message: str, exception: BaseException = None
27
28
the module where the exception was raised (or ``False`` to skip logging the exception)
28
29
29
30
"""
30
- from asphalt .exceptions .api import ExceptionReporter
31
-
32
- assert check_argument_types ()
33
-
34
31
if not exception :
35
32
exception = sys .exc_info ()[1 ]
36
33
if not exception :
37
34
raise ValueError ('missing "exception" parameter and no current exception present in '
38
35
'sys.exc_info()' )
39
36
37
+ actual_logger : logging .Logger | None
40
38
if isinstance (logger , bool ):
41
- if logger :
42
- frame = exception .__traceback__ .tb_frame
39
+ tb = exception .__traceback__
40
+ if logger and tb :
41
+ frame = tb .tb_frame
43
42
module = inspect .getmodule (frame )
44
- if module :
45
- logger = logging .getLogger (module .__spec__ .name )
43
+ if module and module . __spec__ :
44
+ actual_logger = logging .getLogger (module .__spec__ .name )
46
45
else : # pragma: no cover
47
- logger = logging .getLogger (frame .f_globals ['__name__' ])
46
+ actual_logger = logging .getLogger (frame .f_globals ['__name__' ])
48
47
else :
49
- logger = None
48
+ actual_logger = None
50
49
elif isinstance (logger , str ):
51
- logger = logging .getLogger (logger )
50
+ actual_logger = logging .getLogger (logger )
51
+ else :
52
+ actual_logger = logger
52
53
53
- if logger :
54
- logger .error (message , exc_info = exception )
54
+ if actual_logger :
55
+ actual_logger .error (message , exc_info = exception )
55
56
56
57
extras_providers = ctx .get_resources (ExtrasProvider )
57
58
for reporter in ctx .get_resources (ExceptionReporter ):
58
- extra = {} # type: Dict [str, Any]
59
+ extra : dict [str , Any ] = {}
59
60
for provider in extras_providers :
60
61
try :
61
62
new_extra = provider .get_extras (ctx , reporter )
0 commit comments