Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

### Breaking Changes

- Do not honor `policy.on_exception` returning `True` to stop raising any longer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stop raising what any longer? sorry a little confused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When users (e.g. you.:)) implement their own policy, they can return 'True' in on_execepion method to tell the pipeline "the error is handled, no need to raise it any longer". W/ this change, we want to disallow users to do that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"SansIOHTTPPolicy.on_exception returns None instead of bool"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Now we don't honor the return of on_exception any more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Are you suggesting we use
"SansIOHTTPPolicy.on_exception returns None instead of bool"
in changelog?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, just a suggestion for describing the change.


### Bugs Fixed

- UnboundLocalError when SansIOHTTPPolicy handles an exception #15222
- respect text encoding specified in argument (thanks to @ryohji for the contribution) #20796

### Other Changes
Expand Down
6 changes: 1 addition & 5 deletions sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/azure-core/azure/core/pipeline/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 2 additions & 7 deletions sdk/core/azure-core/azure/core/pipeline/policies/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -130,7 +125,7 @@ def on_exception(self, request): # pylint: disable=unused-argument
:language: python
:dedent: 4
"""
return False
return


class RequestHistory(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions sdk/core/azure-core/samples/test_example_sansio.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ def example_on_exception():
try:
response = policy.on_request(request)
except Exception:
if not policy.on_exception(request):
raise
policy.on_exception(request)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider simply removing this example.


# or use
exc_type, exc_value, exc_traceback = sys.exc_info()
Expand Down