Skip to content

Commit

Permalink
fix(error): raise exception when ServerInfo.get fails
Browse files Browse the repository at this point in the history
If ServerInfoItem.from_response gets invalid XML, raise the error
immediately instead of suppressing the error and setting an invalid
version number
  • Loading branch information
jorwoods committed Sep 28, 2024
1 parent c6d74e7 commit 1ec1a5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 3 additions & 5 deletions tableauserverclient/models/server_info_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ def from_response(cls, resp, ns):
try:
parsed_response = fromstring(resp)
except xml.etree.ElementTree.ParseError as error:
logger.info(f"Unexpected response for ServerInfo: {resp}")
logger.info(error)
logger.exception(f"Unexpected response for ServerInfo: {resp}")
return cls("Unknown", "Unknown", "Unknown")
except Exception as error:
logger.info(f"Unexpected response for ServerInfo: {resp}")
logger.info(error)
return cls("Unknown", "Unknown", "Unknown")
logger.exception(f"Unexpected response for ServerInfo: {resp}")
raise error

product_version_tag = parsed_response.find(".//t:productVersion", namespaces=ns)
rest_api_version_tag = parsed_response.find(".//t:restApiVersion", namespaces=ns)
Expand Down
10 changes: 10 additions & 0 deletions test/test_server_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import requests_mock

import tableauserverclient as TSC
from tableauserverclient.server.endpoint.exceptions import NonXMLResponseError

TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")

SERVER_INFO_GET_XML = os.path.join(TEST_ASSET_DIR, "server_info_get.xml")
SERVER_INFO_25_XML = os.path.join(TEST_ASSET_DIR, "server_info_25.xml")
SERVER_INFO_404 = os.path.join(TEST_ASSET_DIR, "server_info_404.xml")
SERVER_INFO_AUTH_INFO_XML = os.path.join(TEST_ASSET_DIR, "server_info_auth_info.xml")
SERVER_INFO_WRONG_SITE = os.path.join(TEST_ASSET_DIR, "server_info_wrong_site.html")


class ServerInfoTests(unittest.TestCase):
Expand Down Expand Up @@ -63,3 +65,11 @@ def test_server_use_server_version_flag(self):
m.get("http://test/api/2.4/serverInfo", text=si_response_xml)
server = TSC.Server("http://test", use_server_version=True)
self.assertEqual(server.version, "2.5")

def test_server_wrong_site(self):
with open(SERVER_INFO_WRONG_SITE, "rb") as f:
response = f.read().decode("utf-8")
with requests_mock.mock() as m:
m.get(self.server.server_info.baseurl, text=response, status_code=404)
with self.assertRaises(NonXMLResponseError):
self.server.server_info.get()

0 comments on commit 1ec1a5f

Please sign in to comment.