Skip to content

Commit

Permalink
clean up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Jan 28, 2021
1 parent e9ccf95 commit 9fca47e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from ._async_paging_method_handler import _AsyncPagingMethodHandler
from ._async_list import AsyncList
from ..paging._utils import ReturnType, ResponseType
from ..paging import PagingMethodABC
from ..exceptions import AzureError
from ..pipeline._tools_async import await_result as _await_result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def get_next(self, continuation_token):
else:
request = self._paging_method.get_next_request(continuation_token, self.initial_request, self._client)
response = await self._make_call(request)
return self._handle_response(continuation_token, response)
return self._handle_response(response)

def extract_data(self, pipeline_response):
continuation_token, list_of_elem = self._extract_data_helper(pipeline_response)
Expand Down
12 changes: 6 additions & 6 deletions sdk/core/azure-core/azure/core/paging/_paging_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def get_continuation_token(self, pipeline_response, deserialized, continuation_t

class ContinueWithCallback(PagingMethodABC):

def __init__(self, next_request_callback, **kwargs): # pylint: disable=unused-argument
def __init__(self, next_request_callback):
"""Base paging method. Accepts the callback for the next request as an init arg.
:param callable next_request_callback: Takes the continuation token as input and
Expand Down Expand Up @@ -184,9 +184,9 @@ def get_continuation_token(self, pipeline_response, deserialized, continuation_t
# check response headers for cont token
return pipeline_response.http_response.headers.get(continuation_token_location, None)

class ContinueWithNextLink(ContinueWithCallback): # pylint: disable=too-many-instance-attributes
class ContinueWithNextLink(ContinueWithCallback):

def __init__(self, path_format_arguments=None, **kwargs):
def __init__(self, path_format_arguments=None):
"""Most common paging method. Uses the continuation token as the URL for the next call.
"""
def _next_request_callback(continuation_token, initial_request, client):
Expand All @@ -196,7 +196,7 @@ def _next_request_callback(continuation_token, initial_request, client):
return request

super(ContinueWithNextLink, self).__init__(
next_request_callback=_next_request_callback, **kwargs
next_request_callback=_next_request_callback
)
self._path_format_arguments = path_format_arguments or {}

Expand All @@ -216,7 +216,7 @@ def get_next_request(self, continuation_token, initial_request, client):

class ContinueWithRequestHeader(ContinueWithCallback):

def __init__(self, header_name, **kwargs):
def __init__(self, header_name):
"""Passes continuation token as a header parameter to next call.
"""
self._header_name = header_name
Expand All @@ -225,7 +225,7 @@ def _next_request_callback(continuation_token, initial_request):
request.headers[self._header_name] = continuation_token
return request
super(ContinueWithRequestHeader, self).__init__(
next_request_callback=_next_request_callback, **kwargs
next_request_callback=_next_request_callback
)

def get_next_request(self, continuation_token, initial_request, client):
Expand Down
18 changes: 6 additions & 12 deletions sdk/core/azure-core/azure/core/paging/_paging_method_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#
# --------------------------------------------------------------------------
import logging
from collections import defaultdict

from ..exceptions import (
map_error,
Expand All @@ -51,12 +50,9 @@ def __init__(
self._client = client
self._initial_state = initial_state
self._cls = kwargs.pop("_cls", None)
self._error_map = defaultdict(
HttpResponseError,
{
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
)
self._error_map = {
401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError
}
self._error_map.update(kwargs.pop('error_map', {}))
self._item_name = kwargs.pop("item_name", "value")
self._continuation_token_location = kwargs.pop("continuation_token_location", None)
Expand All @@ -68,14 +64,12 @@ def initial_request(self):
return self._initial_state.http_response.request
return self._initial_state

def _handle_response(self, continuation_token, response):
def _handle_response(self, response):
http_response = response.http_response
status_code = http_response.status_code
if status_code >= 400:
if self._error_map:
map_error(status_code=status_code, response=http_response, error_map=self._error_map)
map_error(status_code=status_code, response=http_response, error_map=self._error_map)
error = HttpResponseError(response=http_response)
error.continuation_token = continuation_token
raise error
if "request_id" not in self._operation_config:
self._operation_config["request_id"] = response.http_response.request.headers["x-ms-client-request-id"]
Expand Down Expand Up @@ -108,7 +102,7 @@ def get_next(self, continuation_token):
else:
request = self._paging_method.get_next_request(continuation_token, self.initial_request, self._client)
response = self._make_call(request)
return self._handle_response(continuation_token, response)
return self._handle_response(response)

def extract_data(self, pipeline_response):
return self._extract_data_helper(pipeline_response)
13 changes: 7 additions & 6 deletions sdk/core/azure-core/tests/test_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,18 @@ def __init__(
self._raise_on_second_call = raise_on_second_call

def _make_call(self, request):
self._num_calls += 1
if self._num_calls > 1:
if self._validate_next_request:
self._validate_next_request(request)
if self._raise_on_second_call:
pipeline_response.http_response.status_code = 400

pipeline_response.http_request = request
return pipeline_response

def get_next(self, continuation_token):
response = super(_MyPagingMethodHandler, self).get_next(continuation_token)
self._num_calls += 1
if self._num_calls > 1:
if self._validate_next_request:
self._validate_next_request(response.http_request)
if self._raise_on_second_call:
raise HttpResponseError()
return response

def extract_data(self, pipeline_response):
Expand Down

0 comments on commit 9fca47e

Please sign in to comment.