From 833e66d50c2d2cc0ff8290805581b2e1815989a8 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Fri, 4 Feb 2022 04:22:49 +0100 Subject: [PATCH] Added a couple of comments and a star import --- src/bootstraparse/modules/error_mngr.py | 27 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index df3a389..f11f69b 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -4,6 +4,9 @@ import logging import traceback import sys +# Define the error codes +__all__ = ["ParsingError"] + def initlogging(filename=None, loglevel="ERROR", filemode='w', handler=None): """ @@ -31,12 +34,12 @@ def log_exception(exception, level="ERROR"): """ level = level.lower() 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.") 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.debug("A custom RichException has been raised") return @@ -47,13 +50,23 @@ class ParsingError(Exception): 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) self.line = line 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): - 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: - return self.args[0] + return str(self.message)