Skip to content

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

@hazedav

Description

@hazedav

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).

Metadata

Metadata

Assignees

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