Merge "Display message on HTTPException"
This commit is contained in:
@@ -47,6 +47,14 @@ class HTTPException(ClientException):
|
|||||||
self.details = details
|
self.details = details
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
try:
|
||||||
|
data = json.loads(self.details)
|
||||||
|
message = data.get("error_message", {}).get("faultstring")
|
||||||
|
if message:
|
||||||
|
return "%s (HTTP %s) ERROR %s" % (
|
||||||
|
self.__class__.__name__, self.code, message)
|
||||||
|
except (ValueError, TypeError, AttributeError):
|
||||||
|
pass
|
||||||
return "%s (HTTP %s)" % (self.__class__.__name__, self.code)
|
return "%s (HTTP %s)" % (self.__class__.__name__, self.code)
|
||||||
|
|
||||||
|
|
||||||
@@ -65,18 +73,8 @@ class BadRequest(HTTPException):
|
|||||||
code = 400
|
code = 400
|
||||||
|
|
||||||
|
|
||||||
class HTTPBadRequest(BadRequest):
|
class HTTPBadRequest(HTTPException):
|
||||||
|
code = 400
|
||||||
def __str__(self):
|
|
||||||
try:
|
|
||||||
data = json.loads(self.details)
|
|
||||||
message = data.get("error_message", {}).get("faultstring")
|
|
||||||
if message:
|
|
||||||
return "%s (HTTP %s) ERROR %s" % (
|
|
||||||
self.__class__.__name__, self.code, message)
|
|
||||||
except (ValueError, TypeError, AttributeError):
|
|
||||||
pass
|
|
||||||
return super(HTTPBadRequest, self).__str__()
|
|
||||||
|
|
||||||
|
|
||||||
class Unauthorized(HTTPException):
|
class Unauthorized(HTTPException):
|
||||||
@@ -84,8 +82,8 @@ class Unauthorized(HTTPException):
|
|||||||
code = 401
|
code = 401
|
||||||
|
|
||||||
|
|
||||||
class HTTPUnauthorized(Unauthorized):
|
class HTTPUnauthorized(HTTPException):
|
||||||
pass
|
code = 401
|
||||||
|
|
||||||
|
|
||||||
class Forbidden(HTTPException):
|
class Forbidden(HTTPException):
|
||||||
@@ -93,8 +91,8 @@ class Forbidden(HTTPException):
|
|||||||
code = 403
|
code = 403
|
||||||
|
|
||||||
|
|
||||||
class HTTPForbidden(Forbidden):
|
class HTTPForbidden(HTTPException):
|
||||||
pass
|
code = 403
|
||||||
|
|
||||||
|
|
||||||
class NotFound(HTTPException):
|
class NotFound(HTTPException):
|
||||||
@@ -102,8 +100,8 @@ class NotFound(HTTPException):
|
|||||||
code = 404
|
code = 404
|
||||||
|
|
||||||
|
|
||||||
class HTTPNotFound(NotFound):
|
class HTTPNotFound(HTTPException):
|
||||||
pass
|
code = 404
|
||||||
|
|
||||||
|
|
||||||
class HTTPMethodNotAllowed(HTTPException):
|
class HTTPMethodNotAllowed(HTTPException):
|
||||||
@@ -115,8 +113,8 @@ class Conflict(HTTPException):
|
|||||||
code = 409
|
code = 409
|
||||||
|
|
||||||
|
|
||||||
class HTTPConflict(Conflict):
|
class HTTPConflict(HTTPException):
|
||||||
pass
|
code = 409
|
||||||
|
|
||||||
|
|
||||||
class OverLimit(HTTPException):
|
class OverLimit(HTTPException):
|
||||||
@@ -124,8 +122,8 @@ class OverLimit(HTTPException):
|
|||||||
code = 413
|
code = 413
|
||||||
|
|
||||||
|
|
||||||
class HTTPOverLimit(OverLimit):
|
class HTTPOverLimit(HTTPException):
|
||||||
pass
|
code = 413
|
||||||
|
|
||||||
|
|
||||||
class HTTPInternalServerError(HTTPException):
|
class HTTPInternalServerError(HTTPException):
|
||||||
@@ -145,8 +143,8 @@ class ServiceUnavailable(HTTPException):
|
|||||||
code = 503
|
code = 503
|
||||||
|
|
||||||
|
|
||||||
class HTTPServiceUnavailable(ServiceUnavailable):
|
class HTTPServiceUnavailable(HTTPException):
|
||||||
pass
|
code = 503
|
||||||
|
|
||||||
|
|
||||||
#NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
|
#NOTE(bcwaldon): Build a mapping of HTTP codes to corresponding exception
|
||||||
|
|||||||
@@ -19,33 +19,54 @@ from ceilometerclient import exc
|
|||||||
|
|
||||||
from ceilometerclient.tests import utils
|
from ceilometerclient.tests import utils
|
||||||
|
|
||||||
|
HTTPEXCEPTIONS = {'HTTPBadRequest': exc.HTTPBadRequest,
|
||||||
|
'HTTPUnauthorized': exc.HTTPUnauthorized,
|
||||||
|
'HTTPForbidden': exc.HTTPForbidden,
|
||||||
|
'HTTPNotFound': exc.HTTPNotFound,
|
||||||
|
'HTTPMethodNotAllowed': exc.HTTPMethodNotAllowed,
|
||||||
|
'HTTPConflict': exc.HTTPConflict,
|
||||||
|
'HTTPOverLimit': exc.HTTPOverLimit,
|
||||||
|
'HTTPInternalServerError': exc.HTTPInternalServerError,
|
||||||
|
'HTTPNotImplemented': exc.HTTPNotImplemented,
|
||||||
|
'HTTPBadGateway': exc.HTTPBadGateway,
|
||||||
|
'HTTPServiceUnavailable': exc.HTTPServiceUnavailable}
|
||||||
|
|
||||||
class HTTPBadRequestTest(utils.BaseTestCase):
|
|
||||||
|
|
||||||
|
class HTTPExceptionsTest(utils.BaseTestCase):
|
||||||
def test_str_no_details(self):
|
def test_str_no_details(self):
|
||||||
exception = exc.HTTPBadRequest()
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception))
|
exception = v()
|
||||||
|
ret_str = k + " (HTTP " + str(exception.code) + ")"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|
||||||
def test_str_no_json(self):
|
def test_str_no_json(self):
|
||||||
exception = exc.HTTPBadRequest(details="foo")
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception))
|
exception = v(details="foo")
|
||||||
|
ret_str = k + " (HTTP " + str(exception.code) + ")"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|
||||||
def test_str_no_error_message(self):
|
def test_str_no_error_message(self):
|
||||||
exception = exc.HTTPBadRequest(details=json.dumps({}))
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception))
|
exception = v(details=json.dumps({}))
|
||||||
|
ret_str = k + " (HTTP " + str(exception.code) + ")"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|
||||||
def test_str_no_faultstring(self):
|
def test_str_no_faultstring(self):
|
||||||
exception = exc.HTTPBadRequest(
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
details=json.dumps({"error_message": {"foo": "bar"}}))
|
exception = v(
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception))
|
details=json.dumps({"error_message": {"foo": "bar"}}))
|
||||||
|
ret_str = k + " (HTTP " + str(exception.code) + ")"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|
||||||
def test_str_error_message_unknown_format(self):
|
def test_str_error_message_unknown_format(self):
|
||||||
exception = exc.HTTPBadRequest(
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
details=json.dumps({"error_message": "oops"}))
|
exception = v(details=json.dumps({"error_message": "oops"}))
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400)", str(exception))
|
ret_str = k + " (HTTP " + str(exception.code) + ")"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|
||||||
def test_str_faultstring(self):
|
def test_str_faultstring(self):
|
||||||
exception = exc.HTTPBadRequest(
|
for k, v in HTTPEXCEPTIONS.items():
|
||||||
details=json.dumps({"error_message": {"faultstring": "oops"}}))
|
exception = v(details=json.dumps(
|
||||||
self.assertEqual("HTTPBadRequest (HTTP 400) ERROR oops",
|
{"error_message": {"faultstring": "oops"}}))
|
||||||
str(exception))
|
ret_str = k + " (HTTP " + str(exception.code) + ") ERROR oops"
|
||||||
|
self.assertEqual(ret_str, str(exception))
|
||||||
|
|||||||
Reference in New Issue
Block a user