def test_302_found(self): # XXX I'm not sure we want to raise for 300s... failure = Failure(TwistedWebError(302, "found")) error = self.assertRaises(Exception, error_wrapper, failure, None) self.assertEquals(failure.type, type(error)) self.assertTrue(isinstance(error, TwistedWebError)) self.assertEquals(str(error), "302 found")
def error_wrapper(error, errorClass): """ We want to see all error messages from cloud services. Amazon's EC2 says that their errors are accompanied either by a 400-series or 500-series HTTP response code. As such, the first thing we want to do is check to see if the error is in that range. If it is, we then need to see if the error message is an EC2 one. In the event that an error is not a Twisted web error nor an EC2 one, the original exception is raised. """ http_status = 0 if error.check(TwistedWebError): xml_payload = error.value.response if error.value.status: http_status = int(error.value.status) else: error.raiseException() if http_status >= 400: if not xml_payload: error.raiseException() try: fallback_error = errorClass(xml_payload, error.value.status, str(error.value), error.value.response) except (ParseError, AWSResponseParseError): error_message = http.RESPONSES.get(http_status) fallback_error = TwistedWebError(http_status, error_message, error.value.response) raise fallback_error elif 200 <= http_status < 300: return str(error.value) else: error.raiseException()
def test_500(self): failure = Failure(TwistedWebError(500, "internal error")) error = self.assertRaises(Exception, error_wrapper, failure, None) self.assertTrue(isinstance(error, TwistedWebError)) self.assertEquals(str(error), "500 internal error")
def test_204_no_content(self): failure = Failure(TwistedWebError(204, "No content")) wrapped = error_wrapper(failure, None) self.assertEquals(wrapped, "204 No content")
def _fail_response(self, data, response): return fail( failure.Failure(TwistedWebError(response.code, response=data)))
def _check_response(self, data, response): if response.code not in self._ok_status: return failure.Failure( TwistedWebError(response.code, response=data)) return (response, data)