Skip to content
Merged
38 changes: 35 additions & 3 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand Down
Binary file modified JsonPreprocessor/JsonPreprocessor.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions JsonPreprocessor/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

15 changes: 12 additions & 3 deletions config/robotframework_aio/release_items_JsonPreprocessor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://htmlpreview.github.io/?https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/test/JPP_TestUsecases.html>`_
All **JsonPreprocessor** test cases: `JPP_TestUsecases.html <https://htmlpreview.github.io/?https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/test/JPP_TestUsecases.html>`_
"
]
,
Expand Down Expand Up @@ -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 <https://htmlpreview.github.io/?https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/test/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 <https://htmlpreview.github.io/?https://github.com/test-fullautomation/python-jsonpreprocessor/blob/develop/test/JPP_TestUsecases.html>`_
"
]
}
Expand Down
3 changes: 3 additions & 0 deletions packagedoc/additional_docs/History.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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}