Skip to content
Prev Previous commit
Next Next commit
Review feedback
  • Loading branch information
annatisch committed Jul 25, 2019
commit d49db749db39436ee3a9d77ed63fcfa0f0f8ab59
14 changes: 10 additions & 4 deletions sdk/core/azure-core/azure/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
from azure.core.pipeline.transport.base import _HttpResponseBase


def _get_status_code(resonse):
# type: (_HttpResponseBase) -> int
try:
return response.status_code # Requests
except AttributeError:
return response.status # Aiohttp


def raise_with_traceback(exception, *args, **kwargs):
# type: (Callable, Any, Any) -> None
"""Raise exception with a specified traceback.
Expand Down Expand Up @@ -113,10 +121,8 @@ def __init__(self, message=None, response=None, **kwargs):
self.response = response
if response:
self.reason = response.reason
try:
self.status_code = response.status_code # Requests
except AttributeError:
self.status_code = response.status # Aiohttp
self.status_code = _get_status_code(response)

message = message or "Operation returned an invalid status '{}'".format(self.reason)
try:
try:
Expand Down
13 changes: 9 additions & 4 deletions sdk/core/azure-core/azure/core/pipeline/policies/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
ContentDecodePolicyType = TypeVar('ContentDecodePolicyType', bound='ContentDecodePolicy')


def _get_content_type(response):
# type: (PipelineResponse) -> str
try:
return response.content_type.strip().lower()
except AttributeError:
return response.content_type[0].strip().lower()


class HeadersPolicy(SansIOHTTPPolicy):
"""A simple policy that sends the given headers with the request.

Expand Down Expand Up @@ -350,10 +358,7 @@ def deserialize_from_http_generics(cls, response):
# Try to use content-type from headers if available
content_type = None
if response.content_type: # type: ignore
try:
content_type = response.content_type.strip().lower() # type: ignore
except AttributeError:
content_type = response.content_type[0].strip().lower() # type: ignore
content_type = _get_content_type(response)

# Ouch, this server did not declare what it sent...
# Let's guess it's JSON...
Expand Down