Added a couple of comments and a star import

This commit is contained in:
Maximilien Rigaut 2022-02-04 04:22:49 +01:00
parent ef36fa9278
commit 833e66d50c
1 changed files with 20 additions and 7 deletions

View File

@ -4,6 +4,9 @@ import logging
import traceback import traceback
import sys import sys
# Define the error codes
__all__ = ["ParsingError"]
def initlogging(filename=None, loglevel="ERROR", filemode='w', handler=None): def initlogging(filename=None, loglevel="ERROR", filemode='w', handler=None):
""" """
@ -31,12 +34,12 @@ def log_exception(exception, level="ERROR"):
""" """
level = level.lower() level = level.lower()
logging.__getattribute__(level)(traceback.format_exc()) logging.__getattribute__(level)(traceback.format_exc())
if level == "error": if level in ["critical"]:
print("An unrecoverable error occurred, please check the log file for more information.") print("An unrecoverable error occurred, please check the log file for more information.")
sys.exit() sys.exit()
# raise exception # raise exception # Re-raises the exception
if exception.__class__.__name__ == "RichException": if exception.__class__.__name__ == "ParsingError":
logging.error(exception.__str__()) logging.error(exception.__str__())
logging.debug("A custom RichException has been raised") logging.debug("A custom RichException has been raised")
return return
@ -47,13 +50,23 @@ class ParsingError(Exception):
Exception class for parsing errors Exception class for parsing errors
""" """
def __init__(self, message, line=None, column=None): def __init__(self, message=None, line=None, column=None, file=None):
"""
Initializes the ParsingError class with the filename and a line and column number based on the current position in the file (if available)
"""
super().__init__(message) super().__init__(message)
self.line = line self.line = line
self.column = column self.column = column
self.file = file
self.message = message
if self.message is None:
self.message = "A {} error occurred".format(self.__class__.__name__)
def __str__(self): def __str__(self):
if self.line is not None and self.column is not None: """
return "Line " + str(self.line) + ":" + str(self.column) + ": " + self.args[0] Returns a string representation of the ParsingError
"""
if self.line is not None and self.column is not None and self.file is not None:
return "[{}] Line {}:{} {}".format(self.file, self.line, self.column, self.args[0])
else: else:
return self.args[0] return str(self.message)