8000 Improve handling of 500 errors by shinchris · Pull Request #361 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Improve handling of 500 errors #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions tableauserverclient/server/endpoint/datasources_endpoint.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 4 additions & 2 deletions tableauserverclient/server/endpoint/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .exceptions import ServerResponseError, EndpointUnavailableError, ItemTypeNotAllowed
from .exceptions import ServerResponseError, InternalServerError
from functools import wraps

import logging
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tableauserverclient/server/endpoint/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions tableauserverclient/server/endpoint/workbooks_endpoint.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand Down
0