Create class for exception handling. Make a debug flag to print simple for complex messages. modified: listfix.py modified: listfix/__init__.py deleted: listfix/error.py new file: listfix/errors.py
Brian Barto bartobrian@gmail.com
Wed, 30 Mar 2022 15:22:26 -0400
4 files changed,
21 insertions(+),
5 deletions(-)
M
listfix.py
→
listfix.py
@@ -2,7 +2,7 @@ #!/usr/bin/python3
import sys import os -from listfix import Args, DB, Email, error_handler +from listfix import Args, DB, Email, Errors ######################## ## Function Defs@@ -31,7 +31,8 @@ ########################
## Main Program ######################## -sys.excepthook = error_handler +er = Errors(debug=True) +er.set_exception_handler() ## Connect to DB (create DB if needed) and check tables.
M
listfix/__init__.py
→
listfix/__init__.py
@@ -2,4 +2,4 @@
from .db import DB from .email import Email from .args import Args -from .error import error_handler +from .errors import Errors
D
listfix/error.py
@@ -1,2 +0,0 @@
-def error_handler(exception_type, exception, traceback): - print(f"Error: {exception}")
A
listfix/errors.py
@@ -0,0 +1,17 @@
+import sys + +class Errors: + + def __init__(self, debug=False): + self.debug = debug + self._exception_handler = None + + def set_exception_handler(self): + self._exception_handler = sys.excepthook + sys.excepthook = self.exception_handler + + def exception_handler(self, exception_type, exception, traceback): + if (self.debug): + self._exception_handler(exception_type, exception, traceback) + else: + print(f"Error: {exception}")