Skip to content

check_bom.py: Not reliable when printing SW360Error details #227

Description

@rfuentess

check_bom.py: Not reliable when printing SW360Error details

Summary

Python version: 3.11.2

At check_bom.py (_find_by_id()) If SW360Error is raised with error codes other than not_found they are not being displayed at the terminal.

Details

Seen while I was working without proper tokens and trying to figure what was happening.

At the class SW360Error(IOError) we have the following definition for its member Response:

self.response: Optional[Response] = response

and the relevant lines at _find_by_id() at _find_by_id() :

            except SW360Error as swex:
                if swex.response is None:
                    print_red("  Unknown error: " + swex.message)
                elif swex.response.status_code == requests.codes['not_found']:
                    print_yellow(
                        "  Not found " + component.name +
                        ", " + version + ", " + sw360id)
                    break

                # only report other errors if this is the third attempt
                if step >= 2:
                    print(Fore.LIGHTRED_EX + "  Error retrieving release data: ")
                    print(
                        "  " + component.name + ", " + version +
                        ", " + sw360id)
                    if swex.response:
                        print("  Status Code: " + str(swex.response.status_code))
                    if swex.message:
                        print("    Message: " + swex.message)
                    print(Style.RESET_ALL)

At least with python 3.11.2 we should be using if swex.response is not None: instead of if swex.response.

PoC

I did a dirty PoC for verifying it. But I admit that maybe I'm not populating properly the Response object.

from sw360 import SW360Error
from requests import Response

try:
    msg = ""
    r = Response()
    r.status_code = 401
    print(f"Test 1: Response: {r} - message: {msg}")
    raise SW360Error(r, message=msg)
except SW360Error as swex:
    if swex.response:
        print("  Status Code: " + str(swex.response.status_code))
    if swex.message:
            print("    Message: " + swex.message)

try:
    msg = "Pika! Pika!"
    r = Response()
    r.status_code = 401
    print(f"Test 2: Response: {r} - message: {msg}")
    raise SW360Error(r, message=msg)
except SW360Error as swex:
    if swex.response:
        print("  Status Code: " + str(swex.response.status_code))
    if swex.message:
            print("    Message: " + swex.message)

try:
    msg = "Pika! Pika!"
    r = Response()
    r.status_code = 401
    print(f"Test 3: Response: {r} - message: {msg} (AND `if not None`)")
    raise SW360Error(r, message=msg)
except SW360Error as swex:
    if swex.response is not None:
        print("  Status Code: " + str(swex.response.status_code))
    if swex.message:
        print("    Message: " + swex.message)


try:
    msg = ""
    r = None
    print(f"Test 4: Response: {r} - message: {msg} (original if)")
    raise SW360Error(r, message=msg)
except SW360Error as swex:
    if swex.response is not None:
        print("  Status Code: " + str(swex.response.status_code))
    if swex.message:
        print("    Message: " + swex.message)        

try:
    msg = ""
    r = None
    print(f"Test 5: Response: {r} - message: {msg} (AND `if not None`)")
    raise SW360Error(r, message=msg)
except SW360Error as swex:
    if swex.response is not None:
        print("  Status Code: " + str(swex.response.status_code))
    if swex.message:
        print("    Message: " + swex.message)        

Output:

Test 1: Response: <Response [401]> - message: 
Test 2: Response: <Response [401]> - message: Pika! Pika!
    Message: Pika! Pika!
Test 3: Response: <Response [401]> - message: Pika! Pika! (AND `if not None`)
  Status Code: 401
    Message: Pika! Pika!
Test 4: Response: None - message:  (original if)
Test 5: Response: None - message:  (AND `if not None`)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions