Skip to content

Commit 780420b

Browse files
committed
Partially port PyMySQL/PyMySQL#304
aiomysql now reraises the original exception during connect() if it's not `IOError`, `OSError` or `asyncio.TimeoutError`. This was previously always raised as `OperationalError`. fixes #792
1 parent a09398f commit 780420b

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ next (unreleased)
88

99
* Remove deprecated Pool.get #706
1010

11+
* | Partially ported `PyMySQL#304 <https://github.com/PyMySQL/PyMySQL/pull/304>`_ #792
12+
| aiomysql now reraises the original exception during connect() if it's not `IOError`, `OSError` or `asyncio.TimeoutError`.
13+
| This was previously always raised as `OperationalError`.
14+
1115
0.1.1 (2022-05-08)
1216
^^^^^^^^^^^^^^^^^^
1317

aiomysql/connection.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,20 @@ async def _connect(self):
555555
self._writer.transport.close()
556556
self._reader = None
557557
self._writer = None
558-
raise OperationalError(2003,
559-
"Can't connect to MySQL server on %r" %
560-
self._host) from e
558+
559+
# As of 3.11, asyncio.TimeoutError is a deprecated alias of
560+
# OSError. For consistency, we're also considering this an
561+
# OperationalError on earlier python versions.
562+
if isinstance(e, (IOError, OSError, asyncio.TimeoutError)):
563+
raise OperationalError(
564+
CR.CR_CONN_HOST_ERROR,
565+
"Can't connect to MySQL server on %r" % self._host,
566+
) from e
567+
568+
# If e is neither IOError nor OSError, it's a bug.
569+
# Raising AssertionError would hide the original error, so we just
570+
# reraise it.
571+
raise
561572

562573
def _set_keep_alive(self):
563574
transport = self._writer.transport

tests/test_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def fill_my_cnf(mysql_params):
2626

2727
@pytest.mark.run_loop
2828
async def test_connect_timeout(connection_creator):
29-
# All exceptions are caught and raised as operational errors
29+
# OSErrors and asyncio.TimeoutError are caught and raised as operational
30+
# errors
3031
with pytest.raises(aiomysql.OperationalError):
3132
await connection_creator(connect_timeout=0.000000000001)
3233

tests/test_issues.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,13 @@ async def test_issue_323(mysql_server, loop, recwarn):
465465
finally:
466466
async with conn.cursor() as cur:
467467
await cur.execute("DELETE FROM `bugtest`.`testtable`;")
468+
469+
470+
# https://github.com/aio-libs/aiomysql/issues/792
471+
@pytest.mark.run_loop
472+
async def test_issue_792(connection_creator):
473+
with pytest.raises(aiomysql.OperationalError) as exc_info:
474+
await connection_creator(db="does_not_exist")
475+
476+
assert exc_info.value.args[0] == 1049
477+
assert exc_info.value.args[1] == "Unknown database 'does_not_exist'"

0 commit comments

Comments
 (0)