Skip to content

Commit b352912

Browse files
Add query_id in PrestoQueryError
1 parent 4bccbda commit b352912

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

prestodb/client.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ def delete(self, url):
373373
proxies=PROXIES,
374374
)
375375

376-
def _process_error(self, error):
376+
def _process_error(self, error, query_id):
377377
error_type = error['errorType']
378378
if error_type == 'EXTERNAL':
379-
raise exceptions.PrestoExternalError(error)
379+
raise exceptions.PrestoExternalError(error, query_id)
380380
elif error_type == 'USER_ERROR':
381-
return exceptions.PrestoUserError(error)
381+
return exceptions.PrestoUserError(error, query_id)
382382

383-
return exceptions.PrestoQueryError(error)
383+
return exceptions.PrestoQueryError(error, query_id)
384384

385385
def raise_response_error(self, http_response):
386386
if http_response.status_code == 503:
@@ -402,7 +402,7 @@ def process(self, http_response):
402402
response = http_response.json()
403403
logger.debug('HTTP {}: {}'.format(http_response.status_code, response))
404404
if 'error' in response:
405-
raise self._process_error(response['error'])
405+
raise self._process_error(response['error'], response.get('id'))
406406

407407
if constants.HEADER_CLEAR_SESSION in http_response.headers:
408408
for prop in get_header_values(
@@ -505,7 +505,10 @@ def execute(self):
505505
call fetch() until is_finished is true.
506506
"""
507507
if self._cancelled:
508-
raise exceptions.PrestoUserError("Query has been cancelled")
508+
raise exceptions.PrestoUserError(
509+
"Query has been cancelled",
510+
self.query_id,
511+
)
509512

510513
response = self._request.post(self._sql)
511514
status = self._request.process(response)

prestodb/exceptions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class DatabaseError(Exception):
4545

4646

4747
class PrestoQueryError(Exception):
48-
def __init__(self, error):
48+
def __init__(self, error, query_id=None):
4949
self._error = error
50+
self._query_id = query_id
5051

5152
@property
5253
def error_code(self):
@@ -80,12 +81,17 @@ def error_location(self):
8081
location = self._error['errorLocation']
8182
return (location['lineNumber'], location['columnNumber'])
8283

84+
@property
85+
def query_id(self):
86+
return self._query_id
87+
8388
def __repr__(self):
84-
return '{}(type={}, name={}, message="{}")'.format(
89+
return '{}(type={}, name={}, message="{}", query_id={})'.format(
8590
self.__class__.__name__,
8691
self.error_type,
8792
self.error_name,
8893
self.message,
94+
self.query_id,
8995
)
9096

9197
def __str__(self):

tests/test_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ def test_presto_fetch_error(monkeypatch):
452452
assert 'suppressed' in error.failure_info
453453
assert error.message == 'line 1:15: Schema must be specified when session schema is not set'
454454
assert error.error_location == (1, 15)
455+
assert error.query_id == '20161116_205844_00002_xtnym'
455456

456457

457458
@pytest.mark.parametrize("error_code, error_type, error_message", [

0 commit comments

Comments
 (0)