diff --git a/sdk/core/azure-core/CHANGELOG.md b/sdk/core/azure-core/CHANGELOG.md index 58900823d075..4efd94826a82 100644 --- a/sdk/core/azure-core/CHANGELOG.md +++ b/sdk/core/azure-core/CHANGELOG.md @@ -1,13 +1,17 @@ # Release History -## 1.19.2 (Unreleased) +## 1.20.0 (Unreleased) ### Features Added ### Breaking Changes +- SansIOHTTPPolicy.on_exception returns None instead of bool. + ### Bugs Fixed +- UnboundLocalError when SansIOHTTPPolicy handles an exception #15222 + ### Other Changes ## 1.19.1 (2021-11-01) diff --git a/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md b/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md index ce1918a708fd..1f9051960c16 100644 --- a/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md +++ b/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md @@ -347,11 +347,7 @@ def on_response(self, request, response): """Is executed after the request comes back from the policy.""" def on_exception(self, request): - """Is executed if an exception is raised while executing this policy. - - Return True if the exception has been handled and should not - be forwarded to the caller. - """ + """Is executed if an exception is raised while executing this policy.""" ``` SansIOHTTPPolicy methods can be declared as coroutines, but then they can only be used with a AsyncPipeline. diff --git a/sdk/core/azure-core/azure/core/_version.py b/sdk/core/azure-core/azure/core/_version.py index 6c8b9f65e0fd..229982c5253b 100644 --- a/sdk/core/azure-core/azure/core/_version.py +++ b/sdk/core/azure-core/azure/core/_version.py @@ -9,4 +9,4 @@ # regenerated. # -------------------------------------------------------------------------- -VERSION = "1.19.2" +VERSION = "1.20.0" diff --git a/sdk/core/azure-core/azure/core/pipeline/_base.py b/sdk/core/azure-core/azure/core/pipeline/_base.py index b67721dec26f..91e95cedde07 100644 --- a/sdk/core/azure-core/azure/core/pipeline/_base.py +++ b/sdk/core/azure-core/azure/core/pipeline/_base.py @@ -70,8 +70,8 @@ def send(self, request): try: response = self.next.send(request) except Exception: # pylint: disable=broad-except - if not _await_result(self._policy.on_exception, request): - raise + _await_result(self._policy.on_exception, request) + raise else: _await_result(self._policy.on_response, request, response) return response diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py index 228e3fd20f58..07dd229de96d 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication.py @@ -119,9 +119,8 @@ def send(self, request): response = self.next.send(request) self.on_response(request, response) except Exception: # pylint:disable=broad-except - handled = self.on_exception(request) - if not handled: - raise + self.on_exception(request) + raise else: if response.http_response.status_code == 401: self._token = None # any cached token is invalid @@ -132,9 +131,8 @@ def send(self, request): response = self.next.send(request) self.on_response(request, response) except Exception: # pylint:disable=broad-except - handled = self.on_exception(request) - if not handled: - raise + self.on_exception(request) + raise return response @@ -162,18 +160,16 @@ def on_response(self, request, response): """ def on_exception(self, request): - # type: (PipelineRequest) -> bool + # type: (PipelineRequest) -> None """Executed when an exception is raised while executing the next policy. This method is executed inside the exception handler. :param request: The Pipeline request object :type request: ~azure.core.pipeline.PipelineRequest - :return: False by default, override with True to stop the exception. - :rtype: bool """ # pylint: disable=no-self-use,unused-argument - return False + return class AzureKeyCredentialPolicy(SansIOHTTPPolicy): diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication_async.py b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication_async.py index 76564320b742..d6f5250d9b80 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_authentication_async.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_authentication_async.py @@ -115,18 +115,16 @@ def on_response(self, request: "PipelineRequest", response: "PipelineResponse") :type response: ~azure.core.pipeline.PipelineResponse """ - def on_exception(self, request: "PipelineRequest") -> "Union[bool, Awaitable[bool]]": + def on_exception(self, request: "PipelineRequest") -> None: """Executed when an exception is raised while executing the next policy. This method is executed inside the exception handler. :param request: The Pipeline request object :type request: ~azure.core.pipeline.PipelineRequest - :return: False by default, override with True to stop the exception. - :rtype: bool """ # pylint: disable=no-self-use,unused-argument - return False + return def _need_new_token(self) -> bool: return not self._token or self._token.expires_on - time.time() < 300 diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_base.py b/sdk/core/azure-core/azure/core/pipeline/policies/_base.py index 9dc24ad16fa0..abc63894f5ea 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_base.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_base.py @@ -109,18 +109,13 @@ def on_response(self, request, response): # pylint: disable=no-self-use def on_exception(self, request): # pylint: disable=unused-argument - # type: (PipelineRequest) -> Union[bool, Awaitable[bool]] + # type: (PipelineRequest) -> None """Is executed if an exception is raised while executing the next policy. - Developer can optionally implement this method to return True - if the exception has been handled and should not be forwarded to the caller. - This method is executed inside the exception handler. :param request: The Pipeline request object :type request: ~azure.core.pipeline.PipelineRequest - :return: False by default, override with True to stop the exception. - :rtype: bool .. admonition:: Example: @@ -130,7 +125,7 @@ def on_exception(self, request): # pylint: disable=unused-argument :language: python :dedent: 4 """ - return False + return class RequestHistory(object): diff --git a/sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py b/sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py index c1885be92fbc..46d9a07ba448 100644 --- a/sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py +++ b/sdk/core/azure-core/azure/core/pipeline/policies/_distributed_tracing.py @@ -127,7 +127,6 @@ def on_response(self, request, response): # type: (PipelineRequest, PipelineResponse) -> None self.end_span(request, response=response.http_response) - def on_exception(self, request): # pylint: disable=unused-argument - # type: (PipelineRequest) -> bool + def on_exception(self, request): + # type: (PipelineRequest) -> None self.end_span(request, exc_info=sys.exc_info()) - return False diff --git a/sdk/core/azure-core/samples/test_example_sansio.py b/sdk/core/azure-core/samples/test_example_sansio.py index 64add90a2c35..9006aaffa8b2 100644 --- a/sdk/core/azure-core/samples/test_example_sansio.py +++ b/sdk/core/azure-core/samples/test_example_sansio.py @@ -167,17 +167,3 @@ def example_proxy_policy(): # You can also configure proxies by setting the environment variables # HTTP_PROXY and HTTPS_PROXY. # [END proxy_policy] - -def example_on_exception(): - policy = SansIOHTTPPolicy() - request = HttpRequest("GET", "https://bing.com") - # [START on_exception] - try: - response = policy.on_request(request) - except Exception: - if not policy.on_exception(request): - raise - - # or use - exc_type, exc_value, exc_traceback = sys.exc_info() - # [END on_exception]