Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved feedback in case of JSON exceptions wanted #279

Closed
HolQue opened this issue Apr 9, 2024 · 5 comments
Closed

Improved feedback in case of JSON exceptions wanted #279

HolQue opened this issue Apr 9, 2024 · 5 comments
Assignees
Milestone

Comments

@HolQue
Copy link
Collaborator

HolQue commented Apr 9, 2024

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
Copy link
Owner

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

@HolQue
Copy link
Collaborator Author

HolQue commented Apr 10, 2024

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
Copy link
Collaborator

namsonx commented Apr 10, 2024

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 added a commit that referenced this issue Apr 10, 2024
In case of JSON syntax errors the log file contains an extract of the JSON content nearby the position, where the error occurred.
Realizes: #279
HolQue added a commit to test-fullautomation/robotframework-testsuitesmanagement that referenced this issue Apr 10, 2024
In case of JSON syntax errors in configuration files, the Robot Framework log files contain an extract of the JSON content nearby the position, where the error occurred.
Reacts on: test-fullautomation/python-jsonpreprocessor#279
@HolQue
Copy link
Collaborator Author

HolQue commented Apr 11, 2024

Retest successful. Issue can be closed.

@test-fullautomation
Copy link
Owner

integrated in RobotFramework AIO 0.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants