Skip to content

Invalid error handling. #10

@krasoffski

Description

@krasoffski

Summary

  1. Wrong approach for exception message updating.
  2. Useless error handling for invalid requests.
  3. Missing response handler for mult-part form request.

Part 1

At the current moment implemented wrong approach for updating error massage like bellow:

try:
    return json.loads(self.raw)["id"]
except KeyError as error:
    error.message += "Raw: {0}".format(self.raw)
    raise

Let's create a simple example err.py and test it on different versions of python:

try:
    x = {"abc":123}["id"]
except KeyError as error:
    error.message += "Raw: {0}".format("some additional info")
    raise

Here is the output:

$ python2 err.py 
Traceback (most recent call last):
  File "err.py", line 2, in <module>
    x = {"abc":123}["id"]
KeyError: 'id'
$ python3 err.py 
Traceback (most recent call last):
  File "err.py", line 2, in <module>
    x = {"abc":123}["id"]
KeyError: 'id'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "err.py", line 4, in <module>
    error.message += "Raw: {0}".format("some additiona info")
AttributeError: 'KeyError' object has no attribute 'message'

As you can see for python2 there is no any additional info appeared. Moreover, python3 exception does not have attribute with name message (chain exception).

Part 2 (more serious)

No exception appeared in case of invalid request!
Let me show an example err2.py.txt
Create save log request without time field like bellow:

log_rq = SaveLogRQ(
    item_id=test.id,
    # time=timestamp(),
    message="Hello World!",
    level="INFO",
)
service.log(log_rq)

image

If you run attached file, no error is appeared and no log message is created in the Report Portal!

You will get error in case of trying to access id from response object for invalid save log request:

log_rq = SaveLogRQ(item_id=test.id,
                                  # time=timestamp(),
                                  message="Hello World!",
                                  level="INFO")
r = service.log(log_rq)
print(r.id)

Traceback is following:

Traceback (most recent call last):
  File "err2.py.txt", line 49, in <module>
    print(r.id)
  File "./client-python/reportportal_client/model/response.py", line 17, in id
    return json.loads(self.raw)["id"]
KeyError: 'id'

Part 3 (improvements)

Need to add response handler for multi-part form request, because json part represented as an array.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions