Throw exceptions on erroneous status codes

This patch aims to additional exception checking
on receiving other erroneous status codes apart from
400 and 404.

Change-Id: I184c2c0627c457d9c49a84e9e8313a0c88617ec1
This commit is contained in:
Sriram Madapusi Vasudevan 2014-06-04 14:13:38 -04:00
parent 38c8c203f5
commit 11e18a4524
3 changed files with 58 additions and 1 deletions

View File

@ -22,7 +22,9 @@ raised as `ResourceNotFound`
from marconiclient import errors
__all__ = ['TransportError', 'ResourceNotFound', 'MalformedRequest']
__all__ = ['TransportError', 'ResourceNotFound', 'MalformedRequest',
'UnauthorizedError', 'ForbiddenError', 'ServiceUnavailableError',
'InternalServerError']
class TransportError(errors.MarconiError):
@ -41,3 +43,34 @@ class MalformedRequest(TransportError):
This error maps to HTTP's 400
"""
class UnauthorizedError(TransportError):
"""Indicates that a request was not authenticated
This error maps to HTTP's 401
"""
class ForbiddenError(TransportError):
"""Indicates that a request is forbidden
to access the particular resource
This error maps to HTTP's 403
"""
class InternalServerError(TransportError):
"""Indicates that the server encountered
an unexpected situation
This error maps to HTTP's 500
"""
class ServiceUnavailableError(TransportError):
"""Indicates that the server was unable
to service the request
This error maps to HTTP's 503
"""

View File

@ -29,7 +29,11 @@ class HttpTransport(base.Transport):
http_to_marconi = {
400: errors.MalformedRequest,
401: errors.UnauthorizedError,
403: errors.ForbiddenError,
404: errors.ResourceNotFound,
500: errors.InternalServerError,
503: errors.ServiceUnavailableError
}
def __init__(self, options):

View File

@ -23,6 +23,7 @@ from marconiclient.transport import request
class TestHttpTransport(base.TestBase):
"""Tests for the HTTP transport."""
def setUp(self):
@ -79,3 +80,22 @@ class TestHttpTransport(base.TestBase):
params=params,
headers=final_headers,
data=None)
def test_error_handling(self):
params = {'name': 'Opportunity',
'address': 'NASA'}
req = request.Request('http://example.org/',
operation='test_operation',
params=params)
with mock.patch.object(self.transport.client, 'request',
autospec=True) as request_method:
exception_iterator = self.transport.http_to_marconi.items()
for response_code, exception in exception_iterator:
resp = prequest.Response()
resp.status_code = response_code
request_method.return_value = resp
self.assertRaises(exception, lambda: self.transport.send(req))