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

UnboundLocalError when SansIOHTTPPolicy handles an exception #15222

Closed
chlowell opened this issue Nov 11, 2020 · 1 comment
Closed

UnboundLocalError when SansIOHTTPPolicy handles an exception #15222

chlowell opened this issue Nov 11, 2020 · 1 comment
Assignees
Labels
Azure.Core bug This issue requires a change to an existing behavior in the product in order to be resolved.

Comments

@chlowell
Copy link
Member

_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?

@chlowell chlowell added bug This issue requires a change to an existing behavior in the product in order to be resolved. Azure.Core labels Nov 11, 2020
@chlowell
Copy link
Member Author

chlowell commented Jun 3, 2021

A fix for this should apply to BearerTokenCredentialPolicy as well. That policy began as a subclass of SansIOHTTPPolicy and now follows its behavior for compatibility reasons.

openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this issue Jul 16, 2021
Compute 04 01 (Azure#15222)

* sql t2 readme config

* readme config

* Update readme.python.md

* conflient resolve

* readme py config

Co-authored-by: msyyc <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Core bug This issue requires a change to an existing behavior in the product in order to be resolved.
Projects
None yet
Development

No branches or pull requests

2 participants