2
2
# Copyright (c) Microsoft Corporation. All rights reserved.
3
3
# Licensed under the MIT License. See License.txt in the project root for license information.
4
4
# --------------------------------------------------------------------------------------------
5
-
5
+ from contextlib import contextmanager
6
6
from copy import copy
7
7
from typing import TYPE_CHECKING , Dict , Any , Union , List , cast , Type
8
8
from xml .etree .ElementTree import ElementTree , Element
9
9
10
- from azure .core .exceptions import ResourceNotFoundError
10
+ from azure .core .exceptions import ResourceNotFoundError , HttpResponseError
11
11
from azure .core .pipeline import Pipeline
12
12
from azure .core .pipeline .policies import HttpLoggingPolicy , DistributedTracingPolicy , ContentDecodePolicy , \
13
13
RequestIdPolicy , BearerTokenCredentialPolicy
28
28
from azure .core .credentials import TokenCredential # pylint:disable=ungrouped-imports
29
29
30
30
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
+
31
44
def _convert_xml_to_object (queue_name , et , clazz ):
32
45
# type: (str, Union[Element, ElementTree], Type[Model]) -> Union[QueueDescription, QueueRuntimeInfo]
33
46
content_ele = cast (ElementTree , et ).find (constants .CONTENT_TAG )
@@ -96,10 +109,12 @@ def _get_queue_object(self, queue_name, clazz):
96
109
97
110
if not queue_name :
98
111
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
+ )
103
118
return _convert_xml_to_object (queue_name , et , clazz )
104
119
105
120
def get_queue (self , queue_name ):
@@ -129,10 +144,12 @@ def create_queue(self, queue):
129
144
)
130
145
)
131
146
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
+ )
136
153
return _convert_xml_to_object (queue_name , et , QueueDescription )
137
154
138
155
def update_queue (self , queue_description ):
@@ -149,15 +166,16 @@ def update_queue(self, queue_description):
149
166
)
150
167
)
151
168
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
+ )
159
178
)
160
- )
161
179
return _convert_xml_to_object (queue_description .queue_name , et , QueueDescription )
162
180
163
181
def delete_queue (self , queue_name ):
@@ -166,14 +184,17 @@ def delete_queue(self, queue_name):
166
184
167
185
if not queue_name :
168
186
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 )
170
189
171
190
def list_queues (self , skip = 0 , max_count = 100 ):
172
191
# 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
+ )
177
198
entries = et .findall (constants .ENTRY_TAG )
178
199
queue_descriptions = []
179
200
for entry in entries :
0 commit comments