Skip to content

Commit b277dd6

Browse files
committed
Improved error handling
1 parent aae9a93 commit b277dd6

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

test_wls_rest_python.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,23 @@ def test_wls_handle_error_400():
264264
with pytest.raises(wls_rest_python.BadRequestException, match='insert detail here'):
265265
wls_rest_python.WLS._handle_error(response)
266266

267+
def test_wls_handle_error_400_with_alternative_json():
268+
response = MagicMock()
269+
response.status_code = 400
270+
response.json = MagicMock(return_value={
271+
u'status': 400,
272+
u'wls:errorsDetails': [
273+
{
274+
u'o:errorPath': u'machine',
275+
u'type': u'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1',
276+
u'detail': u'Type mismatch. Cannot convert hei to weblogic.management.configuration.MachineMBean.',
277+
u'title': u'FAILURE'
278+
}
279+
],
280+
u'type': u'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1',
281+
u'title': u'ERRORS'})
282+
with pytest.raises(wls_rest_python.BadRequestException, match='Type mismatch'):
283+
wls_rest_python.WLS._handle_error(response)
267284

268285
def test_wls_handle_error_401():
269286
response = MagicMock()
@@ -342,9 +359,9 @@ def test_wls_handle_error_503():
342359
def test_wls_handle_unknown_error():
343360
response = MagicMock()
344361
response.status_code = 507
345-
response.json = MagicMock(return_value={'detail': 'whaat'})
362+
response.json = MagicMock(return_value={'detail': 'Weird error'})
346363
with pytest.raises(
347-
wls_rest_python.WLSException, match='An unknown error occured. Got status code: 507'
364+
wls_rest_python.WLSException, match='Weird error'
348365
):
349366
wls_rest_python.WLS._handle_error(response)
350367

wls_rest_python.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,39 +195,43 @@ def _handle_response(self, response):
195195

196196
@staticmethod
197197
def _handle_error(response):
198+
199+
exception_type = WLSException
200+
exception_message = 'An unknown error occured.'
201+
198202
if response.status_code == 400:
199-
raise BadRequestException(response.json()['detail'])
203+
exception_type = BadRequestException
200204

201205
if response.status_code == 401:
202206
# does not return json
203207
raise UnauthorizedException()
204208

205209
if response.status_code == 403:
206-
raise ForbiddenException(response.json()['detail'])
210+
exception_type = ForbiddenException
207211

208212
if response.status_code == 404:
209-
raise NotFoundException(response.json()['detail'])
213+
exception_type = NotFoundException
210214

211215
if response.status_code == 405:
212-
raise MethodNotAllowedException(response.json()['detail'])
216+
exception_type = MethodNotAllowedException
213217

214218
if response.status_code == 406:
215-
raise NotAcceptableException(response.json()['detail'])
219+
exception_type = NotAcceptableException
216220

217221
if response.status_code == 500:
218-
# may not return json...
219-
try:
220-
raise ServerErrorException(response.json()['detail'])
221-
except ValueError:
222-
pass
223-
raise ServerErrorException(response.text)
222+
exception_type = ServerErrorException
224223

225224
if response.status_code == 503:
226-
raise ServiceUnavailableException(response.json()['detail'])
225+
exception_type = ServiceUnavailableException
227226

228-
raise WLSException(
229-
'An unknown error occured. Got status code: {}'.format(response.status_code)
230-
)
227+
try:
228+
exception_message = response.json()['detail']
229+
except KeyError:
230+
exception_message = response.json()
231+
except ValueError:
232+
exception_message = response.text
233+
234+
raise exception_type(exception_message)
231235

232236

233237
class WLSObject(object):

0 commit comments

Comments
 (0)