Skip to content

Commit 037bc13

Browse files
authored
Merge pull request piccolo-orm#81 from piccolo-orm/asgi_improvements
in the ASGI template, catch connection errors
2 parents b49c38a + 1754c47 commit 037bc13

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

piccolo/apps/asgi/commands/templates/starlette/_fastapi_app.py.jinja

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ async def delete_task(task_id: int):
7676

7777
@app.on_event("startup")
7878
async def open_database_connection_pool():
79-
engine = engine_finder()
80-
await engine.start_connection_pool()
79+
try:
80+
engine = engine_finder()
81+
await engine.start_connection_pool()
82+
except Exception:
83+
print("Unable to connect to the database")
8184

8285

8386
@app.on_event("shutdown")
8487
async def close_database_connection_pool():
85-
engine = engine_finder()
86-
await engine.close_connection_pool()
88+
try:
89+
engine = engine_finder()
90+
await engine.close_connection_pool()
91+
except Exception:
92+
print("Unable to connect to the database")

piccolo/apps/asgi/commands/templates/starlette/_starlette_app.py.jinja

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ app = Starlette(
2929

3030
@app.on_event("startup")
3131
async def open_database_connection_pool():
32-
engine = engine_finder()
33-
await engine.start_connection_pool()
32+
try:
33+
engine = engine_finder()
34+
await engine.start_connection_pool()
35+
except Exception:
36+
print("Unable to connect to the database")
3437

3538

3639
@app.on_event("shutdown")
3740
async def close_database_connection_pool():
38-
engine = engine_finder()
39-
await engine.close_connection_pool()
41+
try:
42+
engine = engine_finder()
43+
await engine.close_connection_pool()
44+
except Exception:
45+
print("Unable to connect to the database")

0 commit comments

Comments
 (0)