From 11e18a4524bbf54e85805d0649739f8e80c473fc Mon Sep 17 00:00:00 2001 From: Sriram Madapusi Vasudevan Date: Wed, 4 Jun 2014 14:13:38 -0400 Subject: [PATCH] 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 --- marconiclient/transport/errors.py | 35 ++++++++++++++++++++++++++++++- marconiclient/transport/http.py | 4 ++++ tests/unit/transport/test_http.py | 20 ++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/marconiclient/transport/errors.py b/marconiclient/transport/errors.py index bca06eee..70023a8a 100644 --- a/marconiclient/transport/errors.py +++ b/marconiclient/transport/errors.py @@ -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 + """ diff --git a/marconiclient/transport/http.py b/marconiclient/transport/http.py index bec1cc91..ecdf19f2 100644 --- a/marconiclient/transport/http.py +++ b/marconiclient/transport/http.py @@ -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): diff --git a/tests/unit/transport/test_http.py b/tests/unit/transport/test_http.py index 6cc273e8..3cc53693 100644 --- a/tests/unit/transport/test_http.py +++ b/tests/unit/transport/test_http.py @@ -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))