Skip to content

Error handling unfavorable #342

@HolQue

Description

@HolQue

The JsonPreprocessor method jsonLoads requires a string as input, e.g.:

jsonpStr = "{\"A\" : 1, \"B\" : 2}"
retValues = json_preprocessor.jsonLoads(jsonpStr)

But if someone - by mistake - provides a dictionary instead of a string:

jsonpStr = {"A" : 1, "B" : 2}
retValues = json_preprocessor.jsonLoads(jsonpStr)

this happens:

Traceback (most recent call last):
  File "C:\workplace\RobotFramework\python39\lib\site-packages\JsonPreprocessor\CJsonPreprocessor.py", line 1527, in jsonLoads
    sJsonData= self.__loadAndRemoveComments(sJsonpContent, isFile=False)
  File "C:\workplace\RobotFramework\python39\lib\site-packages\JsonPreprocessor\CJsonPreprocessor.py", line 393, in __loadAndRemoveComments
    sContentCleaned=re.sub(pattern, replacer, sContent)
  File "C:\workplace\RobotFramework\python39\lib\re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\workplace\ROBFW\TestTestcases\JsonPreprocessor\jpp_examples.py", line 87, in <module>
    retValues = json_preprocessor.jsonLoads(jsonpStr)
  File "C:\workplace\RobotFramework\python39\lib\site-packages\JsonPreprocessor\CJsonPreprocessor.py", line 1530, in jsonLoads
    raise Exception(f"Could not read JSONP content due to: '{reason}'!")
Exception: Could not read JSONP content due to: 'expected string or bytes-like object'!

Means: The error is detected properly , but rather late. This is unfavorable.

I would prefer to have a type check already at the very beginning of jsonLoads, something like:

if not isinstance(sJsonpContent, str):
    raise Exception(f"Expected a string, but got a value of type {type(sJsonpContent)}")

Metadata

Metadata

Assignees

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions