Skip to content

Commit 47bbdd8

Browse files
Shaheer-rossoneri14hashhar
authored andcommitted
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 6eaef4b commit 47bbdd8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

trino/client.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,10 @@ def execute(self, additional_http_headers=None) -> TrinoResult:
787787
if self.cancelled:
788788
raise exceptions.TrinoUserError("Query has been cancelled", self.query_id)
789789

790-
response = self._request.post(self._query, additional_http_headers)
790+
try:
791+
response = self._request.post(self._query, additional_http_headers)
792+
except requests.exceptions.RequestException as e:
793+
raise trino.exceptions.TrinoConnectionError("failed to execute: {}".format(e))
791794
status = self._request.process(response)
792795
self._info_uri = status.info_uri
793796
self._query_id = status.id
@@ -818,7 +821,10 @@ def _update_state(self, status):
818821

819822
def fetch(self) -> List[List[Any]]:
820823
"""Continue fetching data for the current query_id"""
821-
response = self._request.get(self._request.next_uri)
824+
try:
825+
response = self._request.get(self._request.next_uri)
826+
except requests.exceptions.RequestException as e:
827+
raise trino.exceptions.TrinoConnectionError("failed to fetch: {}".format(e))
822828
status = self._request.process(response)
823829
self._update_state(status)
824830
logger.debug(status)
@@ -836,7 +842,10 @@ def cancel(self) -> None:
836842
return
837843

838844
logger.debug("cancelling query: %s", self.query_id)
839-
response = self._request.delete(self._next_uri)
845+
try:
846+
response = self._request.delete(self._next_uri)
847+
except requests.exceptions.RequestException as e:
848+
raise trino.exceptions.TrinoConnectionError("failed to cancel query: {}".format(e))
840849
logger.debug(response)
841850
if response.status_code == requests.codes.no_content:
842851
self._cancelled = True

trino/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class TrinoAuthError(OperationalError):
6767
pass
6868

6969

70+
class TrinoConnectionError(OperationalError):
71+
pass
72+
73+
7074
class TrinoDataError(NotSupportedError):
7175
pass
7276

0 commit comments

Comments
 (0)