@@ -203,12 +203,12 @@ async def create_pool(self) -> asyncpg.Pool[asyncpg.Record]:
203
203
f"4. Your Supabase project is active and the database is online\n "
204
204
)
205
205
206
- logger .error (f"Failed to connect to database: { e } " )
206
+ logger .error (f"FAILED to connect to database: { e } " )
207
207
logger .error (f"Connection details: { host_part } , Project: { self .project_ref } , Region: { self .db_region } " )
208
208
209
209
raise ConnectionError (error_message ) from e
210
210
211
- except OSError as e :
211
+ except ( OSError , asyncpg . InterfaceError , asyncpg . TooManyConnectionsError ) as e :
212
212
# For network-related errors, provide a different message that clearly indicates
213
213
# this is a network/system issue rather than a database configuration problem
214
214
host_part = self .db_url .split ("@" )[1 ].split ("/" )[0 ] if "@" in self .db_url else "unknown"
@@ -228,6 +228,10 @@ async def create_pool(self) -> asyncpg.Pool[asyncpg.Record]:
228
228
logger .error (f"Connection details: { host_part } " )
229
229
raise ConnectionError (error_message ) from e
230
230
231
+ except Exception as e :
232
+ logger .error (f"Failed to connect to database: { e } " )
233
+ raise ConnectionError (f"Failed to connect to database: { e } " ) from e
234
+
231
235
async def ensure_pool (self ) -> None :
232
236
"""Ensure a valid connection pool exists.
233
237
@@ -360,7 +364,6 @@ async def execute_query(
360
364
QueryResult containing the results of all statements
361
365
362
366
Raises:
363
- ConnectionError: If a database connection issue occurs
364
367
QueryError: If the query execution fails
365
368
PermissionError: When user lacks required privileges
366
369
"""
@@ -372,24 +375,37 @@ async def execute_query(
372
375
)
373
376
logger .debug (f"Executing query (readonly={ readonly } ): { truncated_query } " )
374
377
375
- # Define the operation to execute all statements within a transaction
376
- async def execute_all_statements (conn ):
377
- async def transaction_operation ():
378
- results = []
379
- for statement in validated_query .statements :
380
- if statement .query : # Skip statements with no query
381
- result = await self .execute_statement (conn , statement .query )
382
- results .append (result )
383
- else :
384
- logger .warning (f"Statement has no query, statement: { statement } " )
385
- return results
386
-
387
- # Execute the operation within a transaction
388
- results = await self .with_transaction (conn , transaction_operation , readonly )
389
- return QueryResult (results = results )
390
-
391
- # Execute the operation with a connection
392
- return await self .with_connection (execute_all_statements )
378
+ try :
379
+ # Define the operation to execute all statements within a transaction
380
+ async def execute_all_statements (conn : asyncpg .Connection [Any ]) -> QueryResult :
381
+ async def transaction_operation () -> list [StatementResult ]:
382
+ results = []
383
+ for statement in validated_query .statements :
384
+ if statement .query : # Skip statements with no query
385
+ result = await self .execute_statement (conn , statement .query )
386
+ results .append (result )
387
+ else :
388
+ logger .warning (f"Statement has no query, statement: { statement } " )
389
+ return results
390
+
391
+ # Execute the operation within a transaction
392
+ results = await self .with_transaction (conn , transaction_operation , readonly )
393
+ return QueryResult (results = results )
394
+
395
+ # Execute the operation with a connection
396
+ return await self .with_connection (execute_all_statements )
397
+
398
+ except ConnectionError as e :
399
+ logger .error (f"Query execution failed because of connection error: { e } " )
400
+ raise QueryError (f"Query execution failed because of connection error: { str (e )} " ) from e
401
+
402
+ except PermissionError as e :
403
+ logger .error (f"Query execution failed because of permission error: { e } " )
404
+ raise PermissionError (f"Query execution failed because of permission error: { str (e )} " ) from e
405
+
406
+ except QueryError as e :
407
+ logger .error (f"Query execution failed: { e } " )
408
+ raise QueryError (f"Query execution failed: { str (e )} " ) from e
393
409
394
410
async def _handle_postgres_error (self , error : asyncpg .PostgresError ) -> None :
395
411
"""Handle PostgreSQL errors and convert to appropriate exceptions.
@@ -408,10 +424,7 @@ async def _handle_postgres_error(self, error: asyncpg.PostgresError) -> None:
408
424
) from error
409
425
elif isinstance (
410
426
error ,
411
- (
412
- asyncpg .exceptions .UndefinedTableError ,
413
- asyncpg .exceptions .UndefinedColumnError ,
414
- ),
427
+ (asyncpg .exceptions .UndefinedTableError | asyncpg .exceptions .UndefinedColumnError ),
415
428
):
416
429
logger .error (f"Schema error: { error } " )
417
430
raise QueryError (str (error )) from error
0 commit comments