diff --git a/tableauserverclient/server/endpoint/endpoint.py b/tableauserverclient/server/endpoint/endpoint.py index 8c7e93607..2b2bca229 100644 --- a/tableauserverclient/server/endpoint/endpoint.py +++ b/tableauserverclient/server/endpoint/endpoint.py @@ -1,4 +1,4 @@ -from .exceptions import ServerResponseError, InternalServerError +from .exceptions import ServerResponseError, InternalServerError, NonXMLResponseError from functools import wraps from xml.etree.ElementTree import ParseError @@ -69,8 +69,14 @@ def _check_status(self, server_response): try: raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace) except ParseError: - # not an xml error + # This will happen if we get a non-success HTTP code that + # doesn't return an xml error object (like metadata endpoints) + # we convert this to a better exception and pass through the raw + # response body raise NonXMLResponseError(server_response.content) + except Exception as e: + # anything else re-raise here + raise def get_unauthenticated_request(self, url, request_object=None): return self._make_request(self.parent_srv.session.get, url, request_object=request_object) diff --git a/test/test_requests.py b/test/test_requests.py index 80216ec85..67282b6f9 100644 --- a/test/test_requests.py +++ b/test/test_requests.py @@ -5,7 +5,7 @@ import tableauserverclient as TSC -from tableauserverclient.server.endpoint.exceptions import InternalServerError +from tableauserverclient.server.endpoint.exceptions import InternalServerError, NonXMLResponseError class RequestTests(unittest.TestCase): @@ -55,3 +55,11 @@ def test_internal_server_error(self): with requests_mock.mock() as m: m.register_uri('GET', self.server.server_info.baseurl, status_code=500, text=server_response) self.assertRaisesRegexp(InternalServerError, server_response, self.server.server_info.get) + + # Test that non-xml server errors are handled properly + def test_non_xml_error(self): + self.server.version = "3.2" + server_response = "this is not xml" + with requests_mock.mock() as m: + m.register_uri('GET', self.server.server_info.baseurl, status_code=499, text=server_response) + self.assertRaisesRegexp(NonXMLResponseError, server_response, self.server.server_info.get)