Skip to content

Commit

Permalink
Partially port PyMySQL/PyMySQL#304
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Nothing4You committed Aug 23, 2022
1 parent a09398f commit 780420b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ next (unreleased)

* Remove deprecated Pool.get #706

* | Partially ported `PyMySQL#304 <https://github.com/PyMySQL/PyMySQL/pull/304>`_ #792
| aiomysql now reraises the original exception during connect() if it's not `IOError`, `OSError` or `asyncio.TimeoutError`.
| This was previously always raised as `OperationalError`.

0.1.1 (2022-05-08)
^^^^^^^^^^^^^^^^^^

Expand Down
17 changes: 14 additions & 3 deletions aiomysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,20 @@ async def _connect(self):
self._writer.transport.close()
self._reader = None
self._writer = None
raise OperationalError(2003,
"Can't connect to MySQL server on %r" %
self._host) from e

# As of 3.11, asyncio.TimeoutError is a deprecated alias of
# OSError. For consistency, we're also considering this an
# OperationalError on earlier python versions.
if isinstance(e, (IOError, OSError, asyncio.TimeoutError)):
raise OperationalError(
CR.CR_CONN_HOST_ERROR,
"Can't connect to MySQL server on %r" % self._host,
) from e

# If e is neither IOError nor OSError, it's a bug.
# Raising AssertionError would hide the original error, so we just
# reraise it.
raise

def _set_keep_alive(self):
transport = self._writer.transport
Expand Down
3 changes: 2 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def fill_my_cnf(mysql_params):

@pytest.mark.run_loop
async def test_connect_timeout(connection_creator):
# All exceptions are caught and raised as operational errors
# OSErrors and asyncio.TimeoutError are caught and raised as operational
# errors
with pytest.raises(aiomysql.OperationalError):
await connection_creator(connect_timeout=0.000000000001)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,13 @@ async def test_issue_323(mysql_server, loop, recwarn):
finally:
async with conn.cursor() as cur:
await cur.execute("DELETE FROM `bugtest`.`testtable`;")


# https://github.com/aio-libs/aiomysql/issues/792
@pytest.mark.run_loop
async def test_issue_792(connection_creator):
with pytest.raises(aiomysql.OperationalError) as exc_info:
await connection_creator(db="does_not_exist")

assert exc_info.value.args[0] == 1049
assert exc_info.value.args[1] == "Unknown database 'does_not_exist'"

0 comments on commit 780420b

Please sign in to comment.