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

Exception Handling: local variable 'response' referenced before assignment #12

Closed
hazedav opened this issue Feb 22, 2021 · 0 comments
Closed
Assignees

Comments

@hazedav
Copy link
Collaborator

hazedav commented Feb 22, 2021

The exception handling appears to be routing generic exceptions (i.e. Exception) through custom exceptions (i.e. ApiError) however in doing so it is passing response which may have failed to assign.

Take the following example:

        try:
            response = self._session.post(uri, json=data, headers=headers)
    
            # Validate the response
            self._check_response_code(response, DEFAULT_SUCCESS_RESPONSE_CODES)
    
            self._print_debug_response(response)
    
        except Exception:
>           raise ApiError(response)
E           UnboundLocalError: local variable 'response' referenced before assignment

In this case if self._session.post excepts, then response will not be assigned and we do not get proper exception handling. There are a few different approaches that can be taken here (one of which would be to initialize response to None and then handle this accordingly).

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

No branches or pull requests

2 participants