@@ -66,7 +66,8 @@ class Reporter:
6666 raise_after_report : bool
6767
6868 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 ,
69+ formatter : '_log21.CrashReporter.Formatter' = None ,
70+ exceptions_to_catch : _Iterable [BaseException ] = None ,
7071 exceptions_to_ignore : _Iterable [BaseException ] = None ):
7172 """
7273 :param report_function: Function to call when an exception is raised.
@@ -137,6 +138,26 @@ def wrap(*args, **kwargs):
137138
138139 return wrap
139140
141+ def catch (self , exception : BaseException ):
142+ """
143+ Add an exception to the list of exceptions to catch.
144+
145+ :param exception: Exception to catch.
146+ """
147+ if self ._exceptions_to_catch is None :
148+ self ._exceptions_to_catch = set ()
149+ self ._exceptions_to_catch .add (exception )
150+
151+ def ignore (self , exception : BaseException ):
152+ """
153+ Add an exception to the list of exceptions to ignore.
154+
155+ :param exception: Exception to ignore.
156+ """
157+ if self ._exceptions_to_ignore is None :
158+ self ._exceptions_to_ignore = set ()
159+ self ._exceptions_to_ignore .add (exception )
160+
140161
141162class ConsoleReporter (Reporter ):
142163 """
@@ -186,12 +207,13 @@ class ConsoleReporter(Reporter):
186207 """
187208
188209 def __init__ (self , raise_after_report : bool = False , formatter : '_log21.CrashReporter.Formatter' = None ,
189- print_function : _Callable = print ):
210+ print_function : _Callable = print , exceptions_to_catch : _Iterable [BaseException ] = None ,
211+ exceptions_to_ignore : _Iterable [BaseException ] = None ):
190212 """
191213 :param raise_after_report: If True, the exception will be raised after the report_function is called.
192214 :param print_function: Function to use to print the message.
193215 """
194- super ().__init__ (self ._report , raise_after_report )
216+ super ().__init__ (self ._report , raise_after_report , formatter , exceptions_to_catch , exceptions_to_ignore )
195217
196218 if formatter :
197219 if isinstance (formatter , _log21 .CrashReporter .Formatter ):
@@ -203,7 +225,7 @@ def __init__(self, raise_after_report: bool = False, formatter: '_log21.CrashRep
203225
204226 self .print = print_function
205227
206- def _report (self , exception : Exception ):
228+ def _report (self , exception : BaseException ):
207229 """
208230 Prints the exception to the console.
209231
@@ -220,8 +242,10 @@ class FileReporter(Reporter):
220242 """
221243
222244 def __init__ (self , file : _Union [str , _PathLike , _IO ], raise_after_report : bool = True ,
223- formatter : '_log21.CrashReporter.Formatter' = None ):
224- super ().__init__ (self ._report , raise_after_report )
245+ formatter : '_log21.CrashReporter.Formatter' = None ,
246+ exceptions_to_catch : _Iterable [BaseException ] = None ,
247+ exceptions_to_ignore : _Iterable [BaseException ] = None ):
248+ super ().__init__ (self ._report , raise_after_report , formatter , exceptions_to_catch , exceptions_to_ignore )
225249 if isinstance (file , str ):
226250 self .file = open (file , 'a' )
227251 elif isinstance (file , _PathLike ):
@@ -242,7 +266,7 @@ def __init__(self, file: _Union[str, _PathLike, _IO], raise_after_report: bool =
242266 else :
243267 self .formatter = _log21 .CrashReporter .Formatters .Formatter (** _FILE_REPORTER_FORMAT )
244268
245- def _report (self , exception : Exception ):
269+ def _report (self , exception : BaseException ):
246270 """
247271 Writes the exception to the file.
248272
@@ -292,8 +316,10 @@ class EmailReporter(Reporter):
292316 """
293317
294318 def __init__ (self , mail_host : str , port : int , from_address : str , to_address : str , password : str , username : str = '' ,
295- tls : bool = True , raise_after_report : bool = True , formatter : '_log21.CrashReporter.Formatter' = None ):
296- super ().__init__ (self ._report , raise_after_report )
319+ tls : bool = True , raise_after_report : bool = True , formatter : '_log21.CrashReporter.Formatter' = None ,
320+ exceptions_to_catch : _Iterable [BaseException ] = None ,
321+ exceptions_to_ignore : _Iterable [BaseException ] = None ):
322+ super ().__init__ (self ._report , raise_after_report , formatter , exceptions_to_catch , exceptions_to_ignore )
297323 self .mail_host = mail_host
298324 self .port = port
299325 self .from_address = from_address
@@ -328,7 +354,7 @@ def __init__(self, mail_host: str, port: int, from_address: str, to_address: str
328354 else :
329355 self .formatter = _log21 .CrashReporter .Formatters .Formatter (** _EMAIL_REPORTER_FORMAT )
330356
331- def _report (self , exception : Exception ):
357+ def _report (self , exception : BaseException ):
332358 """
333359 Sends an email with the exception.
334360
0 commit comments