add exception error cases

facebook
Dohyun Lim 2026-03-13 11:22:55 +09:00
parent 50796ac743
commit 4fbfbf92a6
1 changed files with 15 additions and 6 deletions

View File

@ -132,13 +132,22 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
try: try:
yield session yield session
except Exception as e: except Exception as e:
import traceback from fastapi import HTTPException
await session.rollback() await session.rollback()
logger.error(traceback.format_exc()) duration = (time.perf_counter() - start_time) * 1000
logger.error( # 클라이언트 에러(4xx)는 WARNING, 서버 에러(5xx)는 ERROR로 구분
f"[get_session] ROLLBACK - error: {type(e).__name__}: {e}, " if isinstance(e, HTTPException) and e.status_code < 500:
f"duration: {(time.perf_counter() - start_time)*1000:.1f}ms" logger.warning(
) f"[get_session] ROLLBACK ({e.status_code}) - "
f"error: {type(e).__name__}: {e}, duration: {duration:.1f}ms"
)
else:
import traceback
logger.error(traceback.format_exc())
logger.error(
f"[get_session] ROLLBACK - error: {type(e).__name__}: {e}, "
f"duration: {duration:.1f}ms"
)
raise e raise e
finally: finally:
total_time = time.perf_counter() - start_time total_time = time.perf_counter() - start_time