55import smtplib as _smtplib # This module is used to send emails.
66
77from os import PathLike as _PathLike
8- from typing import Callable as _Callable , Any as _Any , Union as _Union , IO as _IO
8+ from typing import Callable as _Callable , Any as _Any , Union as _Union , IO as _IO , Set as _Set , Iterable as _Iterable
99from functools import wraps as _wraps
1010from email .mime .text import MIMEText as _MIMEText
1111from email .mime .multipart import MIMEMultipart as _MIMEMultipart
@@ -60,18 +60,23 @@ class Reporter:
6060 >>>
6161 """
6262
63- _reporter_function : _Callable [[Exception ], _Any ] # A function that will be called when an exception is raised.
63+ _reporter_function : _Callable [[BaseException ], _Any ] # A function that will be called when an exception is raised.
64+ _exceptions_to_catch : _Set = None
65+ _exceptions_to_ignore : _Set = None
6466 raise_after_report : bool
6567
66- def __init__ (self , report_function : _Callable [[Exception ], _Any ], raise_after_report : bool = True ,
67- formatter : '_log21.CrashReporter.Formatter' = None ):
68+ def __init__ (self , report_function : _Callable [[BaseException ], _Any ], raise_after_report : bool = False ,
69+ formatter : '_log21.CrashReporter.Formatter' = None , exceptions_to_catch : _Iterable [BaseException ] = None ,
70+ exceptions_to_ignore : _Iterable [BaseException ] = None ):
6871 """
6972 :param report_function: Function to call when an exception is raised.
7073 :param raise_after_report: If True, the exception will be raised after the report_function is called.
7174 """
7275 self ._reporter_function = report_function
7376 self .raise_after_report = raise_after_report
7477 self .formatter = formatter
78+ self ._exceptions_to_catch = set (exceptions_to_catch ) if exceptions_to_catch else None
79+ self ._exceptions_to_ignore = set (exceptions_to_ignore ) if exceptions_to_ignore else None
7580
7681 def reporter (self , func ):
7782 """
@@ -81,14 +86,54 @@ def reporter(self, func):
8186 :return: Wrapped function.
8287 """
8388
84- @_wraps (func )
85- def wrap (* args , ** kwargs ):
86- try :
87- return func (* args , ** kwargs )
88- except Exception as e :
89- self ._reporter_function (e )
90- if self .raise_after_report :
91- raise e
89+ exceptions_to_catch = tuple (self ._exceptions_to_catch ) if self ._exceptions_to_catch else None
90+ exceptions_to_ignore = tuple (self ._exceptions_to_ignore ) if self ._exceptions_to_ignore else None
91+
92+ if exceptions_to_catch and exceptions_to_ignore :
93+ @_wraps (func )
94+ def wrap (* args , ** kwargs ):
95+ try :
96+ return func (* args , ** kwargs )
97+ except BaseException as e :
98+ if isinstance (e , exceptions_to_catch ) and not isinstance (e , exceptions_to_ignore ):
99+ self ._reporter_function (e )
100+ if self .raise_after_report :
101+ raise e
102+ else :
103+ raise e
104+ elif self ._exceptions_to_catch :
105+ @_wraps (func )
106+ def wrap (* args , ** kwargs ):
107+ try :
108+ return func (* args , ** kwargs )
109+ except BaseException as e :
110+ if isinstance (e , exceptions_to_catch ):
111+ self ._reporter_function (e )
112+ if self .raise_after_report :
113+ raise e
114+ else :
115+ raise e
116+ elif self ._exceptions_to_ignore :
117+ @_wraps (func )
118+ def wrap (* args , ** kwargs ):
119+ try :
120+ return func (* args , ** kwargs )
121+ except BaseException as e :
122+ if not isinstance (e , exceptions_to_ignore ):
123+ self ._reporter_function (e )
124+ if self .raise_after_report :
125+ raise e
126+ else :
127+ raise e
128+ else :
129+ @_wraps (func )
130+ def wrap (* args , ** kwargs ):
131+ try :
132+ return func (* args , ** kwargs )
133+ except BaseException as e :
134+ self ._reporter_function (e )
135+ if self .raise_after_report :
136+ raise e
92137
93138 return wrap
94139
0 commit comments