def check_error_response(self, response): """Raise an exception if the response from the SPI was an error. Args: response: A ResponseTuple containing the backend response. Raises: BackendError if the response is an error. """ status_code = int(response.status.split(' ', 1)[0]) if status_code >= 300: raise errors.BackendError(response)
def test_dispatch_spi_error(self): """Check the error response if the SPI returns an error.""" config = json.dumps({ 'name': 'guestbook_api', 'version': 'v1', 'methods': { 'guestbook.get': { 'httpMethod': 'GET', 'path': 'greetings/{gid}', 'rosyMethod': 'MyApi.greetings_get' } } }) request = test_utils.build_request('/_ah/api/foo') self.prepare_dispatch(config) self.mox.StubOutWithMock(self.server, 'call_spi') # The application chose to throw a 404 error. response = dispatcher.ResponseTuple('404 Not Found', [], ('{"state": "APPLICATION_ERROR",' ' "error_message": "Test error"}')) self.server.call_spi(request, mox.IgnoreArg()).AndRaise( errors.BackendError(response)) self.mox.ReplayAll() response = self.server.dispatch(request, self.start_response) self.mox.VerifyAll() expected_response = ( '{\n' ' "error": {\n' ' "code": 404, \n' ' "errors": [\n' ' {\n' ' "domain": "global", \n' ' "message": "Test error", \n' ' "reason": "notFound"\n' ' }\n' ' ], \n' ' "message": "Test error"\n' ' }\n' '}') response = ''.join(response) self.assert_http_match(response, '404 Not Found', [('Content-Length', '%d' % len(expected_response)), ('Content-Type', 'application/json')], expected_response)
def test_dispatch_rpc_error(self): """Test than an RPC call that returns an error is handled properly.""" config = json.dumps({ 'name': 'guestbook_api', 'version': 'v1', 'methods': { 'guestbook.get': { 'httpMethod': 'GET', 'path': 'greetings/{gid}', 'rosyMethod': 'MyApi.greetings_get' } } }) request = test_utils.build_request( '/_ah/api/rpc', '{"method": "foo.bar", "apiVersion": "X", "id": "gapiRpc"}') self.prepare_dispatch(config) self.mox.StubOutWithMock(self.server, 'call_spi') # The application chose to throw a 404 error. response = dispatcher.ResponseTuple( '404 Not Found', [], ('{"state": "APPLICATION_ERROR",' ' "error_message": "Test error"}')) self.server.call_spi(request, mox.IgnoreArg()).AndRaise( errors.BackendError(response)) self.mox.ReplayAll() response = self.server.dispatch(request, self.start_response) self.mox.VerifyAll() expected_response = { 'error': { 'code': 404, 'message': 'Test error', 'data': [{ 'domain': 'global', 'reason': 'notFound', 'message': 'Test error', }] }, 'id': 'gapiRpc' } response = ''.join(response) self.assertEqual('200 OK', self.response_status) self.assertEqual(expected_response, json.loads(response))