Skip to content

Fix: Prevent NameError in finally block - #1

Open
levis-suse wants to merge 1 commit into
jirib:mainfrom
levis-suse:fix-network-error-handling
Open

Fix: Prevent NameError in finally block#1
levis-suse wants to merge 1 commit into
jirib:mainfrom
levis-suse:fix-network-error-handling

Conversation

@levis-suse

Copy link
Copy Markdown

Currently, if requests.get() raises a connection-level exception (such as a ConnectionError or Timeout), the response variable is never assigned.
Because the finally block executes before sys.exit(1) can unwind the stack, attempting to log response.status_code triggers a NameError: name 'response' is not defined.

Proposed fix:
Initialized response = None before the try blocks in both the get_products and search_package functions.
Added an if response is not None: inside the finally blocks to make sure the status code is only logged if the HTTP request successfully returned a response object.

Network failures should now exit cleanly.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants