Categorize rest client exceptions

In order to help to distinguish Client (4xx)
and not client issues the Rest Client exceptions
moved to 3 Category:

- Client 4xx
- Server 5xx
- Other Rest Client exceptions

UnexpectedContentType exception added
to distinguish the 415 case from the Other
situation.

Change-Id: Ib7ee5ce85e14fffd28c70bb9da4fbea87a8a5903
This commit is contained in:
afazekas
2015-05-21 07:12:31 +02:00
committed by Attila Fazekas
parent 8c26c5c050
commit b48f9feb47
3 changed files with 39 additions and 23 deletions

View File

@@ -583,9 +583,8 @@ class RestClient(object):
:rtype: tuple :rtype: tuple
:return: a tuple with the first entry containing the response headers :return: a tuple with the first entry containing the response headers
and the second the response body and the second the response body
:raises InvalidContentType: If the content-type of the response isn't :raises UnexpectedContentType: If the content-type of the response
an expect type or a 415 response code is isn't an expect type
received
:raises Unauthorized: If a 401 response code is received :raises Unauthorized: If a 401 response code is received
:raises Forbidden: If a 403 response code is received :raises Forbidden: If a 403 response code is received
:raises NotFound: If a 404 response code is received :raises NotFound: If a 404 response code is received
@@ -595,6 +594,7 @@ class RestClient(object):
not in the response body not in the response body
:raises RateLimitExceeded: If a 413 response code is received and :raises RateLimitExceeded: If a 413 response code is received and
over_limit is in the response body over_limit is in the response body
:raises InvalidContentType: If a 415 response code is received
:raises UnprocessableEntity: If a 422 response code is received :raises UnprocessableEntity: If a 422 response code is received
:raises InvalidHTTPResponseBody: The response body wasn't valid JSON :raises InvalidHTTPResponseBody: The response body wasn't valid JSON
and couldn't be parsed and couldn't be parsed
@@ -670,7 +670,7 @@ class RestClient(object):
elif ctype.lower() in TXT_ENC: elif ctype.lower() in TXT_ENC:
parse_resp = False parse_resp = False
else: else:
raise exceptions.InvalidContentType(str(resp.status)) raise exceptions.UnexpectedContentType(str(resp.status))
if resp.status == 401: if resp.status == 401:
if parse_resp: if parse_resp:

View File

@@ -53,77 +53,93 @@ class RestClientException(TempestException,
super(RestClientException, self).__init__(message, *args, **kwargs) super(RestClientException, self).__init__(message, *args, **kwargs)
class InvalidHttpSuccessCode(RestClientException): class OtherRestClientException(RestClientException):
pass
class ServerRestClientException(RestClientException):
pass
class ClientRestClientException(RestClientException):
pass
class InvalidHttpSuccessCode(OtherRestClientException):
message = "The success code is different than the expected one" message = "The success code is different than the expected one"
class NotFound(RestClientException): class NotFound(ClientRestClientException):
message = "Object not found" message = "Object not found"
class Unauthorized(RestClientException): class Unauthorized(ClientRestClientException):
message = 'Unauthorized' message = 'Unauthorized'
class Forbidden(RestClientException): class Forbidden(ClientRestClientException):
message = "Forbidden" message = "Forbidden"
class TimeoutException(RestClientException): class TimeoutException(OtherRestClientException):
message = "Request timed out" message = "Request timed out"
class BadRequest(RestClientException): class BadRequest(ClientRestClientException):
message = "Bad request" message = "Bad request"
class UnprocessableEntity(RestClientException): class UnprocessableEntity(ClientRestClientException):
message = "Unprocessable entity" message = "Unprocessable entity"
class RateLimitExceeded(RestClientException): class RateLimitExceeded(ClientRestClientException):
message = "Rate limit exceeded" message = "Rate limit exceeded"
class OverLimit(RestClientException): class OverLimit(ClientRestClientException):
message = "Quota exceeded" message = "Quota exceeded"
class ServerFault(RestClientException): class ServerFault(ServerRestClientException):
message = "Got server fault" message = "Got server fault"
class NotImplemented(RestClientException): class NotImplemented(ServerRestClientException):
message = "Got NotImplemented error" message = "Got NotImplemented error"
class Conflict(RestClientException): class Conflict(ClientRestClientException):
message = "An object with that identifier already exists" message = "An object with that identifier already exists"
class ResponseWithNonEmptyBody(RestClientException): class ResponseWithNonEmptyBody(OtherRestClientException):
message = ("RFC Violation! Response with %(status)d HTTP Status Code " message = ("RFC Violation! Response with %(status)d HTTP Status Code "
"MUST NOT have a body") "MUST NOT have a body")
class ResponseWithEntity(RestClientException): class ResponseWithEntity(OtherRestClientException):
message = ("RFC Violation! Response with 205 HTTP Status Code " message = ("RFC Violation! Response with 205 HTTP Status Code "
"MUST NOT have an entity") "MUST NOT have an entity")
class InvalidHTTPResponseBody(RestClientException): class InvalidHTTPResponseBody(OtherRestClientException):
message = "HTTP response body is invalid json or xml" message = "HTTP response body is invalid json or xml"
class InvalidHTTPResponseHeader(RestClientException): class InvalidHTTPResponseHeader(OtherRestClientException):
message = "HTTP response header is invalid" message = "HTTP response header is invalid"
class InvalidContentType(RestClientException): class InvalidContentType(ClientRestClientException):
message = "Invalid content type provided" message = "Invalid content type provided"
class UnexpectedResponseCode(RestClientException): class UnexpectedContentType(OtherRestClientException):
message = "Unexpected content type provided"
class UnexpectedResponseCode(OtherRestClientException):
message = "Unexpected response code received" message = "Unexpected response code received"

View File

@@ -464,7 +464,7 @@ class TestRestClientErrorCheckerTEXT(TestRestClientErrorCheckerJSON):
# This test is required only in one exemplar # This test is required only in one exemplar
# Any response code, that bigger than 400, and not in # Any response code, that bigger than 400, and not in
# (401, 403, 404, 409, 413, 422, 500, 501) # (401, 403, 404, 409, 413, 422, 500, 501)
self.assertRaises(exceptions.InvalidContentType, self.assertRaises(exceptions.UnexpectedContentType,
self.rest_client._error_checker, self.rest_client._error_checker,
**self.set_data("405", enc="fake_enc")) **self.set_data("405", enc="fake_enc"))