Skip to content

Commit e76133f

Browse files
committed
chore: update exception type and exception message
1 parent 27c2da8 commit e76133f

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

google/cloud/sql/connector/connector.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from google.cloud.sql.connector.resolver import DnsResolver
4444
from google.cloud.sql.connector.utils import format_database_user
4545
from google.cloud.sql.connector.utils import generate_keys
46+
from google.cloud.sql.connector.exceptions import ClosedConnectorError
4647

4748
logger = logging.getLogger(name=__name__)
4849

@@ -243,8 +244,12 @@ def connect(
243244
# connect runs sync database connections on background thread.
244245
# Async database connections should call 'connect_async' directly to
245246
# avoid hanging indefinitely.
247+
248+
# Check if the connector is closed before attempting to connect.
246249
if self._closed:
247-
raise RuntimeError("Cannot connect using a closed Connector.")
250+
raise ClosedConnectorError(
251+
"Connection attempt failed because the connector has already been closed."
252+
)
248253
connect_future = asyncio.run_coroutine_threadsafe(
249254
self.connect_async(instance_connection_string, driver, **kwargs),
250255
self._loop,
@@ -286,7 +291,9 @@ async def connect_async(
286291
Connector.
287292
"""
288293
if self._closed:
289-
raise RuntimeError("Cannot connect using a closed Connector.")
294+
raise ClosedConnectorError(
295+
"Connection attempt failed because the connector has already been closed."
296+
)
290297
if self._keys is None:
291298
self._keys = asyncio.create_task(generate_keys())
292299
if self._client is None:

google/cloud/sql/connector/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,10 @@ class CacheClosedError(Exception):
8484
Exception to be raised when a ConnectionInfoCache can not be accessed after
8585
it is closed.
8686
"""
87+
88+
89+
class ClosedConnectorError(Exception):
90+
"""
91+
Exception to be raised when a Connector is closed and connect method is
92+
called on it.
93+
"""

tests/unit/test_connector.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import asyncio
1818
import os
19-
import time
2019
from typing import Union
2120

2221
from aiohttp import ClientResponseError
@@ -31,6 +30,7 @@
3130
from google.cloud.sql.connector.connection_name import ConnectionName
3231
from google.cloud.sql.connector.exceptions import CloudSQLIPTypeError
3332
from google.cloud.sql.connector.exceptions import IncompatibleDriverError
33+
from google.cloud.sql.connector.exceptions import ClosedConnectionError
3434
from google.cloud.sql.connector.instance import RefreshAheadCache
3535

3636

@@ -481,17 +481,18 @@ async def test_connect_async_closed_connector(
481481
) as connector:
482482
connector._client = fake_client
483483
await connector.close_async()
484-
# wait for close to complete
485-
# await asyncio.sleep(0.1)
486-
with pytest.raises(RuntimeError) as exc_info:
484+
with pytest.raises(ClosedConnectionError) as exc_info:
487485
await connector.connect_async(
488486
"test-project:test-region:test-instance",
489487
"asyncpg",
490488
user="my-user",
491489
password="my-pass",
492490
db="my-db",
493491
)
494-
assert exc_info.value.args[0] == "Cannot connect using a closed Connector."
492+
assert (
493+
exc_info.value.args[0]
494+
== "Connection attempt failed because the connector has already been closed."
495+
)
495496

496497

497498
def test_connect_closed_connector(
@@ -501,13 +502,15 @@ def test_connect_closed_connector(
501502
with Connector(credentials=fake_credentials) as connector:
502503
connector._client = fake_client
503504
connector.close()
504-
# time.sleep(3.1)
505-
with pytest.raises(RuntimeError) as exc_info:
505+
with pytest.raises(ClosedConnectionError) as exc_info:
506506
connector.connect(
507507
"test-project:test-region:test-instance",
508508
"pg8000",
509509
user="my-user",
510510
password="my-pass",
511511
db="my-db",
512512
)
513-
assert exc_info.value.args[0] == "Cannot connect using a closed Connector."
513+
assert (
514+
exc_info.value.args[0]
515+
== "Connection attempt failed because the connector has already been closed."
516+
)

0 commit comments

Comments
 (0)