Skip to content

Commit e0c19d1

Browse files
committed
Better connection errors handling
1 parent dfe82f1 commit e0c19d1

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/quota/connect_pg.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any:
1313
"""Initialize connection to PostgreSQL database."""
1414
logger.info("Connecting to PostgreSQL storage")
15-
connection = psycopg2.connect(
16-
host=config.host,
17-
port=config.port,
18-
user=config.user,
19-
password=config.password.get_secret_value(),
20-
dbname=config.db,
21-
sslmode=config.ssl_mode,
22-
# sslrootcert=config.ca_cert_path,
23-
gssencmode=config.gss_encmode,
24-
)
25-
if connection is not None:
26-
connection.autocommit = True
27-
return connection
15+
try:
16+
connection = psycopg2.connect(
17+
host=config.host,
18+
port=config.port,
19+
user=config.user,
20+
password=config.password.get_secret_value(),
21+
dbname=config.db,
22+
sslmode=config.ssl_mode,
23+
# sslrootcert=config.ca_cert_path,
24+
gssencmode=config.gss_encmode,
25+
)
26+
if connection is not None:
27+
connection.autocommit = True
28+
return connection
29+
except psycopg2.Error as e:
30+
logger.exception("Error connecting to PostgreSQL database:\n%s", e)
31+
raise

src/quota/connect_sqlite.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any:
1717
connection = None
1818
try:
1919
connection = sqlite3.connect(database=config.db_path)
20-
except sqlite3.Error as e:
2120
if connection is not None:
22-
connection.close()
21+
connection.autocommit = True
22+
return connection
23+
except sqlite3.Error as e:
2324
logger.exception("Error initializing SQLite cache:\n%s", e)
2425
raise
25-
connection.autocommit = True
26-
return connection

0 commit comments

Comments
 (0)