diff --git a/JsonPreprocessor/CJsonPreprocessor.py b/JsonPreprocessor/CJsonPreprocessor.py index 2b625faa..2e45d9ec 100644 --- a/JsonPreprocessor/CJsonPreprocessor.py +++ b/JsonPreprocessor/CJsonPreprocessor.py @@ -197,6 +197,27 @@ def __init__(self, syntax: CSyntaxType = CSyntaxType.python , currentCfg : dict self.jsonCheck = {} self.JPGlobals = {} + def __getFailedJsonDoc(jsonDecodeError=None, areaBeforePosition=50, areaAfterPosition=20, oneLine=True): + failedJsonDoc = None + if jsonDecodeError is None: + return failedJsonDoc + try: + jsonDoc = jsonDecodeError.doc + except: + # 'jsonDecodeError' seems not to be a JSON exception object ('doc' not available) + return failedJsonDoc + jsonDocSize = len(jsonDoc) + positionOfError = jsonDecodeError.pos + if areaBeforePosition > positionOfError: + areaBeforePosition = positionOfError + if areaAfterPosition > (jsonDocSize - positionOfError): + areaAfterPosition = jsonDocSize - positionOfError + failedJsonDoc = jsonDoc[positionOfError-areaBeforePosition:positionOfError+areaAfterPosition] + failedJsonDoc = f"... {failedJsonDoc} ..." + if oneLine is True: + failedJsonDoc = failedJsonDoc.replace("\n", r"\n") + return failedJsonDoc + def __reset(self) -> None: """ Reset initial variables which are set in constructor method after master JSON file is loaded. @@ -684,7 +705,6 @@ def __updateAndReplaceNestedParam(self, oJson : dict, bNested : bool = False, re Output JSON object as dictionary with all variables resolved. """ - def __jsonUpdated(k, v, oJson, bNested, keyNested = '', bDuplicatedHandle=False, recursive = False): if keyNested != '': if not bDuplicatedHandle and keyNested in oJson.keys(): @@ -1281,7 +1301,13 @@ def __handleLastElement(sInput : str) -> str: object_pairs_hook=self.__processImportFiles) except Exception as error: self.__reset() - raise Exception(f"JSON file: {jFile}\n{error}") + failedJsonDoc = self.__getFailedJsonDoc(error) + jsonException = "not defined" + if failedJsonDoc is None: + jsonException = f"{error}\nIn file: '{jFile}'" + else: + jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'" + raise Exception(jsonException) self.bDuplicatedKeys = True # Load Json object with checking duplicated keys feature is enabled. @@ -1292,7 +1318,13 @@ def __handleLastElement(sInput : str) -> str: object_pairs_hook=self.__processImportFiles) except Exception as error: self.__reset() - raise Exception(f"JSON file: {jFile}\n{error}") + failedJsonDoc = self.__getFailedJsonDoc(error) + jsonException = "not defined" + if failedJsonDoc is None: + jsonException = f"${error}\nIn file: '{jFile}'" + else: + jsonException = f"${error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'" + raise Exception(jsonException) self.__checkDotInParamName(oJson) __checkKeynameFormat(oJson) diff --git a/JsonPreprocessor/JsonPreprocessor.pdf b/JsonPreprocessor/JsonPreprocessor.pdf index b9af70cf..31a3ccea 100644 Binary files a/JsonPreprocessor/JsonPreprocessor.pdf and b/JsonPreprocessor/JsonPreprocessor.pdf differ diff --git a/JsonPreprocessor/version.py b/JsonPreprocessor/version.py index 510f6ae6..58af7b5b 100644 --- a/JsonPreprocessor/version.py +++ b/JsonPreprocessor/version.py @@ -18,6 +18,6 @@ # # Version and date of JsonPreprocessor # -VERSION = "0.4.0" -VERSION_DATE = "15.03.2024" +VERSION = "0.5.0" +VERSION_DATE = "10.04.2024" diff --git a/config/robotframework_aio/release_items_JsonPreprocessor.json b/config/robotframework_aio/release_items_JsonPreprocessor.json index f79e3495..584d5cac 100644 --- a/config/robotframework_aio/release_items_JsonPreprocessor.json +++ b/config/robotframework_aio/release_items_JsonPreprocessor.json @@ -61,7 +61,7 @@ * Improved index handling together with nested parameters * Self test extended by further test cases - All JsonPreprocessor test cases: `JPP_TestUsecases.html `_ + All **JsonPreprocessor** test cases: `JPP_TestUsecases.html `_ " ] , @@ -90,9 +90,18 @@ Finally, the value of ``var1`` is 2 and the value of ``var2`` is 3. -* Self test extended by further test cases +* Self test extended by further test cases. + + Overview about **JsonPreprocessor** test cases: `JPP_TestUsecases.html `_ + +* Extended debugging support: + + In case of JSON syntax errors, the **JsonPreprocessor** exception contains an extract of the JSON content nearby the position, where the error occurred. + Line breaks in JSON content are replaced by '``\\n``'. + + | ``'Expecting ',' delimiter: line 3 column 4 (char 43)`` + | ``Nearby: '... \u007b\\n \"dict_param\" : \u007b\"A\" : 1 , \"B\" : 2\u007d\\n \"list_param\" : [\"A\", ...'`` - All JsonPreprocessor test cases: `JPP_TestUsecases.html `_ " ] } diff --git a/packagedoc/additional_docs/History.tex b/packagedoc/additional_docs/History.tex index a930fc15..44820d59 100644 --- a/packagedoc/additional_docs/History.tex +++ b/packagedoc/additional_docs/History.tex @@ -55,4 +55,7 @@ - Optimized errors handling while loading nested parameters\newline - Fixed bugs} +\historyversiondate{0.5.0}{04/2024} +\historychange{Extended debugging support. In case of JSON syntax errors, the JsonPreprocessor exception contains an extract of the JSON content nearby the position, where the error occurred.} + \end{packagehistory}