Skip to content

Commit 1c73ab2

Browse files
committed
chore: update exception type and exception message
1 parent 0216a06 commit 1c73ab2

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
@@ -44,6 +44,7 @@
4444
from google.cloud.sql.connector.resolver import DnsResolver
4545
from google.cloud.sql.connector.utils import format_database_user
4646
from google.cloud.sql.connector.utils import generate_keys
47+
from google.cloud.sql.connector.exceptions import ClosedConnectorError
4748

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

@@ -244,8 +245,12 @@ def connect(
244245
# connect runs sync database connections on background thread.
245246
# Async database connections should call 'connect_async' directly to
246247
# avoid hanging indefinitely.
248+
249+
# Check if the connector is closed before attempting to connect.
247250
if self._closed:
248-
raise RuntimeError("Cannot connect using a closed Connector.")
251+
raise ClosedConnectorError(
252+
"Connection attempt failed because the connector has already been closed."
253+
)
249254
connect_future = asyncio.run_coroutine_threadsafe(
250255
self.connect_async(instance_connection_string, driver, **kwargs),
251256
self._loop,
@@ -287,7 +292,9 @@ async def connect_async(
287292
Connector.
288293
"""
289294
if self._closed:
290-
raise RuntimeError("Cannot connect using a closed Connector.")
295+
raise ClosedConnectorError(
296+
"Connection attempt failed because the connector has already been closed."
297+
)
291298
if self._keys is None:
292299
self._keys = asyncio.create_task(generate_keys())
293300
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)