Skip to content

Commit 9d417d6

Browse files
Wrap all requests.exceptions.RequestException as TrinoConnectionError
Before this change if a user needed to catch connection errors (e.g. connection refused) they needed to add a dependency on requests and import the relevant exception from requests.exceptions module. After this change all requests exception get rethrown as a TrinoConnectionError instead.
1 parent b7055ff commit 9d417d6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

trino/client.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,10 @@ def execute(self, additional_http_headers=None) -> TrinoResult:
803803
if self.cancelled:
804804
raise exceptions.TrinoUserError("Query has been cancelled", self.query_id)
805805

806-
response = self._request.post(self._query, additional_http_headers)
806+
try:
807+
response = self._request.post(self._query, additional_http_headers)
808+
except requests.exceptions.RequestException as e:
809+
raise trino.exceptions.TrinoConnectionError("failed to execute: {}".format(e))
807810
status = self._request.process(response)
808811
self._info_uri = status.info_uri
809812
self._query_id = status.id
@@ -834,7 +837,10 @@ def _update_state(self, status):
834837

835838
def fetch(self) -> List[List[Any]]:
836839
"""Continue fetching data for the current query_id"""
837-
response = self._request.get(self._request.next_uri)
840+
try:
841+
response = self._request.get(self._request.next_uri)
842+
except requests.exceptions.RequestException as e:
843+
raise trino.exceptions.TrinoConnectionError("failed to fetch: {}".format(e))
838844
status = self._request.process(response)
839845
self._update_state(status)
840846
logger.debug(status)
@@ -852,7 +858,10 @@ def cancel(self) -> None:
852858
return
853859

854860
logger.debug("cancelling query: %s", self.query_id)
855-
response = self._request.delete(self._next_uri)
861+
try:
862+
response = self._request.delete(self._next_uri)
863+
except requests.exceptions.RequestException as e:
864+
raise trino.exceptions.TrinoConnectionError("failed to cancel query: {}".format(e))
856865
logger.debug(response)
857866
if response.status_code == requests.codes.no_content:
858867
self._cancelled = True

trino/exceptions.py

+4
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,7 @@ class Http503Error(HttpError):
149149

150150
class Http504Error(HttpError):
151151
pass
152+
153+
154+
class TrinoConnectionError(HttpError):
155+
pass

0 commit comments

Comments
 (0)