Skip to content

UnboundLocalError when SansIOHTTPPolicy handles an exception #15222

@chlowell

Description

@chlowell

_SansIOHTTPPolicyRunner allows a SansIOHTTPPolicy to stop it propagating pipeline exceptions, by returning True from its on_exception method. However, when a policy does this, _SansIOHTTPPolicyRunner raises UnboundLocalError:

from azure.core.pipeline import Pipeline
from azure.core.pipeline.transport import HttpRequest
from azure.core.pipeline.policies import HTTPPolicy, SansIOHTTPPolicy

class ReproPolicy(SansIOHTTPPolicy):
    def on_exception(self, request):
        return True

class ErrorPolicy(HTTPPolicy):
    def send(self, request):
        raise Exception("oops")

pipeline = Pipeline(transport=object(), policies=[ReproPolicy(), ErrorPolicy()])
pipeline.run(HttpRequest("GET", "..."))
Traceback (most recent call last):
  ...
  File "c:\code\azure-sdk-for-python\sdk\core\azure-core\azure\core\pipeline\_base.py", line 77, in send
    return response
UnboundLocalError: local variable 'response' referenced before assignment

... which makes sense because an exception prevents the assignment:

try:
response = self.next.send(request)
except Exception: # pylint: disable=broad-except
if not _await_result(self._policy.on_exception, request):
raise
else:
_await_result(self._policy.on_response, request, response)
return response

I opened an issue rather than a PR because the fix requires some design. Should _SansIOHTTPPolicyRunner return a PipelineResponse with no http_response in this case?

Metadata

Metadata

Assignees

Labels

Azure.CorebugThis issue requires a change to an existing behavior in the product in order to be resolved.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions