From d588117de7a5aec3708f75e670c6a2f87954a50a Mon Sep 17 00:00:00 2001 From: Chris Shin Date: Wed, 14 Nov 2018 13:28:10 -0800 Subject: [PATCH] adding new exception to handle 500 errors properly --- .../server/endpoint/datasources_endpoint.py | 11 +++++++++-- tableauserverclient/server/endpoint/endpoint.py | 6 ++++-- tableauserverclient/server/endpoint/exceptions.py | 9 +++++++++ .../server/endpoint/workbooks_endpoint.py | 12 ++++++++++-- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tableauserverclient/server/endpoint/datasources_endpoint.py b/tableauserverclient/server/endpoint/datasources_endpoint.py index 904d27144..4d7a20b70 100644 --- a/tableauserverclient/server/endpoint/datasources_endpoint.py +++ b/tableauserverclient/server/endpoint/datasources_endpoint.py @@ -1,5 +1,5 @@ from .endpoint import Endpoint, api, parameter_added_in -from .exceptions import MissingRequiredFieldError +from .exceptions import InternalServerError, MissingRequiredFieldError from .fileuploads_endpoint import Fileuploads from .resource_tagger import _ResourceTagger from .. import RequestFactory, DatasourceItem, PaginationItem, ConnectionItem @@ -196,7 +196,14 @@ def publish(self, datasource_item, file_path, mode, connection_credentials=None, file_contents, connection_credentials, connections) - server_response = self.post_request(url, xml_request, content_type) + + # Send the publishing request to server + try: + server_response = self.post_request(url, xml_request, content_type) + except InternalServerError as err: + if err.code == 504 and not as_job: + err.content = "Timeout error while publishing. Please use asynchronous publishing to avoid timeouts." + raise err if as_job: new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0] diff --git a/tableauserverclient/server/endpoint/endpoint.py b/tableauserverclient/server/endpoint/endpoint.py index 994d2133d..f16c9f8df 100644 --- a/tableauserverclient/server/endpoint/endpoint.py +++ b/tableauserverclient/server/endpoint/endpoint.py @@ -1,4 +1,4 @@ -from .exceptions import ServerResponseError, EndpointUnavailableError, ItemTypeNotAllowed +from .exceptions import ServerResponseError, InternalServerError from functools import wraps import logging @@ -62,7 +62,9 @@ def _make_request(self, method, url, content=None, request_object=None, def _check_status(self, server_response): logger.debug(self._safe_to_log(server_response)) - if server_response.status_code not in Success_codes: + if server_response.status_code >= 500: + raise InternalServerError(server_response) + elif server_response.status_code not in Success_codes: raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace) def get_unauthenticated_request(self, url, request_object=None): diff --git a/tableauserverclient/server/endpoint/exceptions.py b/tableauserverclient/server/endpoint/exceptions.py index d77cdea3e..080eca9c8 100644 --- a/tableauserverclient/server/endpoint/exceptions.py +++ b/tableauserverclient/server/endpoint/exceptions.py @@ -21,6 +21,15 @@ def from_response(cls, resp, ns): return error_response +class InternalServerError(Exception): + def __init__(self, server_response): + self.code = server_response.status_code + self.content = server_response.content + + def __str__(self): + return "\n\nError status code: {0}\n{1}".format(self.code, self.content) + + class MissingRequiredFieldError(Exception): pass diff --git a/tableauserverclient/server/endpoint/workbooks_endpoint.py b/tableauserverclient/server/endpoint/workbooks_endpoint.py index 79b15f379..e4d7da466 100644 --- a/tableauserverclient/server/endpoint/workbooks_endpoint.py +++ b/tableauserverclient/server/endpoint/workbooks_endpoint.py @@ -1,5 +1,5 @@ from .endpoint import Endpoint, api, parameter_added_in -from .exceptions import MissingRequiredFieldError +from .exceptions import InternalServerError, MissingRequiredFieldError from .fileuploads_endpoint import Fileuploads from .resource_tagger import _ResourceTagger from .. import RequestFactory, WorkbookItem, ConnectionItem, ViewItem, PaginationItem @@ -256,7 +256,15 @@ def publish(self, workbook_item, file_path, mode, connection_credentials=None, c connection_credentials=conn_creds, connections=connections) logger.debug('Request xml: {0} '.format(xml_request[:1000])) - server_response = self.post_request(url, xml_request, content_type) + + # Send the publishing request to server + try: + server_response = self.post_request(url, xml_request, content_type) + except InternalServerError as err: + if err.code == 504 and not as_job: + err.content = "Timeout error while publishing. Please use asynchronous publishing to avoid timeouts." + raise err + if as_job: new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0] logger.info('Published {0} (JOB_ID: {1}'.format(filename, new_job.id))