Skip to content

Commit 49adde2

Browse files
Added exceptions_to_catch and exceptions_to_ignore arguments to log21.CrashReporter.Reporter class.
1 parent 77a604e commit 49adde2

5 files changed

Lines changed: 65 additions & 16 deletions

File tree

CHANGES-LOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md)
66
Changes log
77
-----------
88

9+
### 2.3.7
10+
11+
Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.
12+
913
### 2.3.6
1014

1115
Added `Print` logging level.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.6
56+
### 2.3.7
5757

58-
Added `Print` logging level.
58+
Added `exceptions_to_catch` and `exceptions_to_ignore` arguments to `log21.CrashReporter.Reporter` class.
5959

6060
[Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md)
6161

log21/CrashReporter/Reporters.py

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import smtplib as _smtplib # This module is used to send emails.
66

77
from 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
99
from functools import wraps as _wraps
1010
from email.mime.text import MIMEText as _MIMEText
1111
from 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

log21/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
2222
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color
2323

24-
__version__ = "2.3.6"
24+
__version__ = "2.3.7"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = file.read()
88

99
DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.'
10-
VERSION = '2.3.6'
10+
VERSION = '2.3.7'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)