Skip to content

Commit 2989b03

Browse files
kislyukUbuntu
authored andcommitted
Update handling of error codes and response size error for latest Data API version
Fixes #21
1 parent f5d1829 commit 2989b03

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

aurora_data_api/__init__.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,13 @@ def _prepare_execute_args(self, operation):
202202
def _format_parameter_set(self, parameters):
203203
return [self.prepare_param(k, v) for k, v in parameters.items()]
204204

205-
def _get_database_error(self, original_error):
206-
# TODO: avoid SHOW ERRORS if on postgres (it's a useless network roundtrip)
207-
try:
208-
err_res = self._client.execute_statement(**self._prepare_execute_args("SHOW ERRORS"))
209-
err_info = self._render_response(err_res)["records"][-1]
210-
return DatabaseError(MySQLErrorCodes(err_info[1]), err_info[2])
211-
except self._client.exceptions.BadRequestException:
212-
return DatabaseError(original_error)
205+
def _get_database_error(self, origin_error):
206+
if getattr(origin_error, "response", {}).get("Error", {}).get("Message", "").startswith("Database error code"):
207+
assert origin_error.response["Error"]["Message"].startswith("Database error code")
208+
code, msg = (s.split(": ", 1)[1] for s in origin_error.response["Error"]["Message"].split(". ", 1))
209+
return DatabaseError(MySQLErrorCodes(int(code)), msg)
210+
else:
211+
return DatabaseError(origin_error)
213212

214213
def execute(self, operation, parameters=None):
215214
self._current_response, self._iterator, self._paging_state = None, None, None
@@ -226,7 +225,7 @@ def execute(self, operation, parameters=None):
226225
except self._client.exceptions.BadRequestException as e:
227226
if "Please paginate your query" in str(e):
228227
self._start_paginated_query(execute_statement_args)
229-
elif "Database response exceeded size limit" in str(e):
228+
elif "Database returned more than the allowed response size limit" in str(e):
230229
self._start_paginated_query(execute_statement_args, records_per_page=max(1, self.arraysize // 2))
231230
else:
232231
raise self._get_database_error(e) from e
@@ -319,7 +318,8 @@ def __iter__(self):
319318
try:
320319
page = self._client.execute_statement(**next_page_args)
321320
except self._client.exceptions.BadRequestException as e:
322-
if "Database response exceeded size limit" in str(e) and self._paging_state["records_per_page"] > 1:
321+
cur_rpp = self._paging_state["records_per_page"]
322+
if "Database returned more than the allowed response size limit" in str(e) and cur_rpp > 1:
323323
self.scroll(-self._paging_state["records_per_page"]) # Rewind the cursor to read the page again
324324
logger.debug("Halving records per page")
325325
self._paging_state["records_per_page"] //= 2

test/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_rowcount(self):
135135

136136
with aurora_data_api.connect(database=self.db_name) as conn, conn.cursor() as cur:
137137
cur.execute("select * from aurora_data_api_test limit 9000")
138-
self.assertEqual(cur.rowcount, 2048 if self.using_mysql else -1)
138+
self.assertEqual(cur.rowcount, 2048)
139139

140140
if self.using_mysql:
141141
return

0 commit comments

Comments
 (0)