Skip to content

Commit 8d9ec08

Browse files
committed
Put response error details into a new response error
1 parent 2923f01 commit 8d9ec08

File tree

2 files changed

+69
-44
lines changed

2 files changed

+69
-44
lines changed

sdk/servicebus/azure-servicebus/azure/servicebus/management/_management_client.py

+44-23
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
5-
5+
from contextlib import contextmanager
66
from copy import copy
77
from typing import TYPE_CHECKING, Dict, Any, Union, List, cast, Type
88
from xml.etree.ElementTree import ElementTree, Element
99

10-
from azure.core.exceptions import ResourceNotFoundError
10+
from azure.core.exceptions import ResourceNotFoundError, HttpResponseError
1111
from azure.core.pipeline import Pipeline
1212
from azure.core.pipeline.policies import HttpLoggingPolicy, DistributedTracingPolicy, ContentDecodePolicy, \
1313
RequestIdPolicy, BearerTokenCredentialPolicy
@@ -28,6 +28,19 @@
2828
from azure.core.credentials import TokenCredential # pylint:disable=ungrouped-imports
2929

3030

31+
@contextmanager
32+
def _handle_response_error():
33+
try:
34+
yield
35+
except HttpResponseError as response_error:
36+
new_response_error = HttpResponseError(
37+
message=response_error.model.detail,
38+
response=response_error.response,
39+
model=response_error.model
40+
)
41+
raise new_response_error
42+
43+
3144
def _convert_xml_to_object(queue_name, et, clazz):
3245
# type: (str, Union[Element, ElementTree], Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
3346
content_ele = cast(ElementTree, et).find(constants.CONTENT_TAG)
@@ -96,10 +109,12 @@ def _get_queue_object(self, queue_name, clazz):
96109

97110
if not queue_name:
98111
raise ValueError("queue_name must be a non-empty str")
99-
et = cast(
100-
ElementTree,
101-
self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
102-
)
112+
113+
with _handle_response_error():
114+
et = cast(
115+
ElementTree,
116+
self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
117+
)
103118
return _convert_xml_to_object(queue_name, et, clazz)
104119

105120
def get_queue(self, queue_name):
@@ -129,10 +144,12 @@ def create_queue(self, queue):
129144
)
130145
)
131146
request_body = create_entity_body.serialize(is_xml=True)
132-
et = cast(
133-
ElementTree,
134-
self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
135-
)
147+
148+
with _handle_response_error():
149+
et = cast(
150+
ElementTree,
151+
self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
152+
)
136153
return _convert_xml_to_object(queue_name, et, QueueDescription)
137154

138155
def update_queue(self, queue_description):
@@ -149,15 +166,16 @@ def update_queue(self, queue_description):
149166
)
150167
)
151168
request_body = create_entity_body.serialize(is_xml=True)
152-
et = cast(
153-
ElementTree,
154-
self._impl.queue.put(
155-
queue_description.queue_name, # type: ignore
156-
request_body,
157-
api_version=constants.API_VERSION,
158-
if_match="*"
169+
with _handle_response_error():
170+
et = cast(
171+
ElementTree,
172+
self._impl.queue.put(
173+
queue_description.queue_name, # type: ignore
174+
request_body,
175+
api_version=constants.API_VERSION,
176+
if_match="*"
177+
)
159178
)
160-
)
161179
return _convert_xml_to_object(queue_description.queue_name, et, QueueDescription)
162180

163181
def delete_queue(self, queue_name):
@@ -166,14 +184,17 @@ def delete_queue(self, queue_name):
166184

167185
if not queue_name:
168186
raise ValueError("queue_name must not be None or empty")
169-
self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
187+
with _handle_response_error():
188+
self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
170189

171190
def list_queues(self, skip=0, max_count=100):
172191
# type: (int, int) -> List[QueueDescription]
173-
et = cast(
174-
ElementTree,
175-
self._impl.list_entities(entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION)
176-
)
192+
193+
with _handle_response_error():
194+
et = cast(
195+
ElementTree,
196+
self._impl.list_entities(entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION)
197+
)
177198
entries = et.findall(constants.ENTRY_TAG)
178199
queue_descriptions = []
179200
for entry in entries:

sdk/servicebus/azure-servicebus/azure/servicebus/management/aio/_management_client_async.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
5-
65
from copy import copy
76
from typing import TYPE_CHECKING, Dict, Any, Union, List, Type, cast
87
from xml.etree.ElementTree import ElementTree, Element
@@ -22,7 +21,7 @@
2221
from .._generated.aio._service_bus_management_client_async import ServiceBusManagementClient \
2322
as ServiceBusManagementClientImpl
2423
from .. import _constants as constants
25-
from .._management_client import _convert_xml_to_object
24+
from .._management_client import _convert_xml_to_object, _handle_response_error
2625
from ._shared_key_policy_async import AsyncServiceBusSharedKeyCredentialPolicy
2726

2827

@@ -88,10 +87,11 @@ async def _get_queue_object(self, queue_name, clazz):
8887
# type: (str, Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
8988
if not queue_name:
9089
raise ValueError("queue_name must be a non-empty str")
91-
et = cast(
92-
ElementTree,
93-
await self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
94-
)
90+
with _handle_response_error():
91+
et = cast(
92+
ElementTree,
93+
await self._impl.queue.get(queue_name, enrich=False, api_version=constants.API_VERSION)
94+
)
9595
return _convert_xml_to_object(queue_name, et, clazz)
9696

9797
async def get_queue(self, queue_name):
@@ -120,10 +120,11 @@ async def create_queue(self, queue):
120120
)
121121
)
122122
request_body = create_entity_body.serialize(is_xml=True)
123-
et = cast(
124-
ElementTree,
125-
await self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
126-
)
123+
with _handle_response_error():
124+
et = cast(
125+
ElementTree,
126+
await self._impl.queue.put(queue_name, request_body, api_version=constants.API_VERSION)
127+
)
127128
return _convert_xml_to_object(queue_name, et, QueueDescription)
128129

129130
async def update_queue(self, queue_description):
@@ -140,12 +141,13 @@ async def update_queue(self, queue_description):
140141
)
141142
)
142143
request_body = create_entity_body.serialize(is_xml=True)
143-
et = cast(
144-
ElementTree,
145-
await self._impl.queue.put(
146-
queue_description.queue_name, request_body, api_version=constants.API_VERSION, if_match="*"
144+
with _handle_response_error():
145+
et = cast(
146+
ElementTree,
147+
await self._impl.queue.put(
148+
queue_description.queue_name, request_body, api_version=constants.API_VERSION, if_match="*"
149+
)
147150
)
148-
)
149151
return _convert_xml_to_object(queue_description.queue_name, et, QueueDescription)
150152

151153
async def delete_queue(self, queue_name):
@@ -154,16 +156,18 @@ async def delete_queue(self, queue_name):
154156

155157
if not queue_name:
156158
raise ValueError("queue_name must not be None or empty")
157-
await self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
159+
with _handle_response_error():
160+
await self._impl.queue.delete(queue_name, api_version=constants.API_VERSION)
158161

159162
async def list_queues(self, skip=0, max_count=100):
160163
# type: (int, int) -> List[QueueDescription]
161-
et = cast(
162-
ElementTree,
163-
await self._impl.list_entities(
164-
entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION
164+
with _handle_response_error():
165+
et = cast(
166+
ElementTree,
167+
await self._impl.list_entities(
168+
entity_type="queues", skip=skip, top=max_count, api_version=constants.API_VERSION
169+
)
165170
)
166-
)
167171
entries = et.findall(constants.ENTRY_TAG)
168172
queue_descriptions = []
169173
for entry in entries:

0 commit comments

Comments
 (0)