|
36 | 36 |
|
37 | 37 | logger = logging.getLogger(__name__) |
38 | 38 |
|
39 | | -postgresql_error_reg = re.compile(r'^ERROR:\s(.*)\s(?:[\s+]+Position:\s([0-9]+);\s)?SQLState:\s([0-9A-Z]+)$') |
40 | | - |
41 | 39 |
|
42 | 40 | class AuroraDataAPIClient: |
43 | 41 | def __init__(self, dbname=None, aurora_cluster_arn=None, secret_arn=None, rds_data_client=None, charset=None, |
@@ -212,18 +210,24 @@ def _prepare_execute_args(self, operation): |
212 | 210 | def _format_parameter_set(self, parameters): |
213 | 211 | return [self.prepare_param(k, v) for k, v in parameters.items()] |
214 | 212 |
|
215 | | - def _get_database_error(self, origin_error): |
216 | | - error_msg = getattr(origin_error, "response", {}).get("Error", {}).get("Message", "") |
| 213 | + def _get_database_error(self, original_error): |
| 214 | + error_msg = getattr(original_error, "response", {}).get("Error", {}).get("Message", "") |
217 | 215 | try: |
218 | 216 | if error_msg.startswith("Database error code"): # MySQL error |
219 | 217 | code, msg = (s.split(": ", 1)[1] for s in error_msg.split(". ", 1)) |
220 | | - return DatabaseError(MySQLErrorCodes(int(code)), msg) |
221 | | - elif error_msg.startswith("ERROR: "): # Postgresql error |
222 | | - msg, pos, code = postgresql_error_reg.match(error_msg.replace('\n', '')).groups() |
223 | | - return DatabaseError(PostgreSQLErrorCodes(code), msg, pos) |
| 218 | + mysql_error_code = MySQLErrorCodes(int(code)) |
| 219 | + return DatabaseError(mysql_error_code, msg) |
| 220 | + elif error_msg.startswith("ERROR: "): # PostgreSQL error |
| 221 | + error_msg = error_msg[len("ERROR: "):] |
| 222 | + error_lines = error_msg.splitlines() |
| 223 | + if error_lines[-1].startswith(" Position: ") and " SQLState: " in error_lines[-1]: |
| 224 | + position, sqlstate = (i.split(":", 1)[1].strip() for i in error_lines[-1].strip().split(";")) |
| 225 | + postgres_error_code = PostgreSQLErrorCodes(sqlstate) |
| 226 | + return DatabaseError(postgres_error_code, "\n".join(error_lines[:-1]), int(position)) |
| 227 | + raise Exception("unable to parse postgresql error") |
224 | 228 | except Exception: |
225 | 229 | pass |
226 | | - return DatabaseError(origin_error) |
| 230 | + return DatabaseError(original_error) |
227 | 231 |
|
228 | 232 | def execute(self, operation, parameters=None): |
229 | 233 | self._current_response, self._iterator, self._paging_state = None, None, None |
|
0 commit comments