8000 adding new exception to handle 500 errors properly (#361) · SnarkyPapi/server-client-python@78a3538 · GitHub
[go: up one dir, main page]

Skip to content

Commit 78a3538

Browse files
authored
adding new exception to handle 500 errors properly (tableau#361)
1 parent cdb0f67 commit 78a3538

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

tableauserverclient/server/endpoint/datasources_endpoint.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .endpoint import Endpoint, api, parameter_added_in
2-
from .exceptions import MissingRequiredFieldError
2+
from .exceptions import InternalServerError, MissingRequiredFieldError
33
from .fileuploads_endpoint import Fileuploads
44
from .resource_tagger import _ResourceTagger
55
from .. import RequestFactory, DatasourceItem, PaginationItem, ConnectionItem
@@ -196,7 +196,14 @@ def publish(self, datasource_item, file_path, mode, connection_credentials=None,
196196
file_contents,
197197
connection_credentials,
198198
connections)
199-
server_response = self.post_request(url, xml_request, content_type)
199+
200+
# Send the publishing request to server
201+
try:
202+
server_response = self.post_request(url, xml_request, content_type)
203+
except InternalServerError as err:
204+
if err.code == 504 and not as_job:
205+
err.content = "Timeout error while publishing. Please use asynchronous publishing to avoid timeouts."
206+
raise err
200207

201208
if as_job:
202209
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]

tableauserverclient/server/endpoint/endpoint.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .exceptions import ServerResponseError, EndpointUnavailableError, ItemTypeNotAllowed
1+
from .exceptions import ServerResponseError, InternalServerError
22
from functools import wraps
33

44
import logging
@@ -62,7 +62,9 @@ def _make_request(self, method, url, content=None, request_object=None,
6262

6363
def _check_status(self, server_response):
6464
logger.debug(self._safe_to_log(server_response))
65-
if server_response.status_code not in Success_codes:
65+
if server_response.status_code >= 500:
66+
raise InternalServerError(server_response)
67+
elif server_response.status_code not in Success_codes:
6668
raise ServerResponseError.from_response(server_response.content, self.parent_srv.namespace)
6769

6870
def get_unauthenticated_request(self, url, request_object=None):

tableauserverclient/server/endpoint/exceptions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ def from_response(cls, resp, ns):
2121
return error_response
2222

2323

24+
class InternalServerError(Exception):
25+
def __init__(self, server_response):
26+
self.code = server_response.status_code
27+
self.content = server_response.content
28+
29+
def __str__(self):
30+
return "\n\nError status code: {0}\n{1}".format(self.code, self.content)
31+
32+
2433
class MissingRequiredFieldError(Exception):
2534
pass
2635

tableauserverclient/server/endpoint/workbooks_endpoint.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .endpoint import Endpoint, api, parameter_added_in
2-
from .exceptions import MissingRequiredFieldError
2+
from .exceptions import InternalServerError, MissingRequiredFieldError
33
from .fileuploads_endpoint import Fileuploads
44
from .resource_tagger import _ResourceTagger
55
from .. import RequestFactory, WorkbookItem, ConnectionItem, ViewItem, PaginationItem
@@ -256,7 +256,15 @@ def publish(self, workbook_item, file_path, mode, connection_credentials=None, c
256256
connection_credentials=conn_creds,
257257
connections=connections)
258258
logger.debug('Request xml: {0} '.format(xml_request[:1000]))
259-
server_response = self.post_request(url, xml_request, content_type)
259+
260+
# Send the publishing request to server
261+
try:
262+
server_response = self.post_request(url, xml_request, content_type)
263+
except InternalServerError as err:
264+
if err.code == 504 and not as_job:
265+
err.content = "Timeout error while publishing. Please use asynchronous publishing to avoid timeouts."
266+
raise err
267+
260268
if as_job:
261269
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
262270
logger.info('Published {0} (JOB_ID: {1}'.format(filename, new_job.id))

0 commit comments

Comments
 (0)
0