forked from rommapp/romm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
27 lines (21 loc) · 831 Bytes
/
Copy pathdatabase.py
File metadata and controls
27 lines (21 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import functools
from fastapi import HTTPException, status
from sqlalchemy.exc import ProgrammingError
from handler.database.base_handler import sync_session
from logger.logger import log
def begin_session(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Reuse a caller-provided session so the handler can join an existing unit of work
if kwargs.get("session") is not None:
return func(*args, **kwargs)
try:
with sync_session.begin() as s:
kwargs["session"] = s
return func(*args, **kwargs)
except ProgrammingError as exc:
log.critical(str(exc))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc
return wrapper