test-fullautomation / python-jsonpreprocessor

A preprocessor for json files
Apache License 2.0
2 stars 2 forks source link

Improved feedback in case of JSON exceptions wanted #279

Closed HolQue closed 4 weeks ago

HolQue commented 1 month ago

I always hesitate about JSON errors like that:

Expecting value: line 13 column 15 (char 522)

or Expecting ',' delimiter: line 4 column 4 (char 76)

How to find the indicated position? What exactly is the JSON code that is used as reference for the counting? Also in case of imported JSON files are involved. Also in case of the entire JSON code is placed in one single line only. Does the counting include code that is commented out?

In my opinion a better support is required here. This is my solution:

def get_failed_json_doc(json_decode_error=None, area_before_position=40, area_after_position=10, one_line=True):
   failed_json_doc = "(detailed information not available)"
   if json_decode_error is None:
      return failed_json_doc
   try:
      json_doc = json_decode_error.doc
   except:
      # 'json_decode_error' seems not to be a JSON exception object
      return failed_json_doc
   json_doc_size     = len(json_doc)
   position_of_error = json_decode_error.pos
   if area_before_position > position_of_error:
      area_before_position = position_of_error
   if area_after_position > (json_doc_size - position_of_error):
      area_after_position = json_doc_size - position_of_error
   failed_json_doc = json_doc[position_of_error-area_before_position:position_of_error+area_after_position]
   failed_json_doc = f"... {failed_json_doc} ..."
   if one_line is True:
      failed_json_doc = failed_json_doc.replace("\n", r"\n")
   return failed_json_doc

It's simple to adapt the JsonPreprocessor to use this function.

Original code:

except Exception as error:
    self.__reset()
    raise Exception(f"JSON file: {jFile}\n{error}")

Adaption:

except Exception as error:
    self.__reset()
    failed_json_doc = get_failed_json_doc(error)
    raise Exception(f"JSON file: {jFile}\n{error}\nNearby: '{failed_json_doc}'")

If this is JSON code containing an error (123 not encapsulated in quotes):

{
   "indexP" : 0,
   "keyP"   : "A",
   "dictP"  : {"A" : 0, "B" : 1},
   "listP"  : ["A", "B"],

   "params" : {123 : "002",
               "003" : ["004", {"005" : "006",
                              "007" : ["008", ["009", "010"]],
                              ...

This is the result:

Expecting property name enclosed in double quotes: line 6 column 16 (char 113)
Nearby: '...   "listP"  : ["A", "B"],\n   "params" : {123 : "002 ...''

What do you think?

test-fullautomation commented 1 month ago

Hi @namsonx , can you please look into this together with Holger. Would be for 0.11.0 Thank you, Thomas

HolQue commented 1 month ago

Hi Son,

my function require some adaptions. I will change the implementation and make a pull request for the JsonPreprocessor within today. Then it's on you to make a review and check for side effects that possibly I have overlooked.

After this we can decide if this already for 0.11 or not.

namsonx commented 1 month ago

Hello Holger,

Your solution looks great especially for end users who will know where they made a mistake in JSON configuration files.

Sure, we will check for side effects then decide if it already for 0.11 or not.

Thank you, Son

HolQue commented 1 month ago

Retest successful. Issue can be closed.

test-fullautomation commented 4 weeks ago

integrated in RobotFramework AIO 0.11.0