Skip to content

Commit 7f53771

Browse files
committed
feat(backend): improve logging and error handling in request and database operations
- Enhance request logging middleware to include response time and error details. - Add logging for the list_roadmaps function to track pagination and item retrieval. - Implement error handling in the database connection function to log database errors and ensure rollback on exceptions.
1 parent c86ee89 commit 7f53771

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

commitly-backend/app/api/roadmap.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,37 @@ async def list_roadmaps(
5656
service: RoadmapService = Depends(get_roadmap_service),
5757
) -> CatalogPage:
5858
"""List catalog with filters, sorting, and pagination."""
59+
import logging
5960
import math
6061

61-
items, total_count = await service.list_catalog(
62-
page=page,
63-
page_size=page_size,
64-
language=language,
65-
tag=tag,
66-
difficulty=difficulty,
67-
min_rating=min_rating,
68-
min_views=min_views,
69-
min_syncs=min_syncs,
70-
sort=sort,
71-
)
62+
logger = logging.getLogger(__name__)
63+
try:
64+
logger.info(f"list_roadmaps: Starting with page={page}, page_size={page_size}")
65+
items, total_count = await service.list_catalog(
66+
page=page,
67+
page_size=page_size,
68+
language=language,
69+
tag=tag,
70+
difficulty=difficulty,
71+
min_rating=min_rating,
72+
min_views=min_views,
73+
min_syncs=min_syncs,
74+
sort=sort,
75+
)
76+
logger.info(f"list_roadmaps: Retrieved {len(items)} items, total={total_count}")
7277

73-
total_pages = math.ceil(total_count / page_size) if total_count > 0 else 0
78+
total_pages = math.ceil(total_count / page_size) if total_count > 0 else 0
7479

75-
return CatalogPage(
76-
items=items,
77-
page=page,
78-
page_size=page_size,
79-
total_count=total_count,
80-
total_pages=total_pages,
81-
)
80+
return CatalogPage(
81+
items=items,
82+
page=page,
83+
page_size=page_size,
84+
total_count=total_count,
85+
total_pages=total_pages,
86+
)
87+
except Exception as e:
88+
logger.error(f"list_roadmaps: Error - {type(e).__name__}: {e}", exc_info=True)
89+
raise
8290

8391

8492
@router.get("/cached/{owner}/{repo}", response_model=RoadmapResponse)

commitly-backend/app/core/database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@ def get_db() -> Generator:
3737
db = SessionLocal()
3838
try:
3939
yield db
40+
except Exception as e:
41+
logger.error(f"Database error in get_db: {e}", exc_info=True)
42+
db.rollback()
43+
raise
4044
finally:
4145
db.close()

commitly-backend/app/main.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,25 @@ class RequestLoggingMiddleware(BaseHTTPMiddleware):
9494
"""Log all incoming requests for debugging."""
9595

9696
async def dispatch(self, request: Request, call_next):
97+
import time
98+
start_time = time.time()
9799
logger.info(
98100
f"Request: {request.method} {request.url.path} "
99101
f"from {request.client.host if request.client else 'unknown'}"
100102
)
101103
try:
102104
response = await call_next(request)
105+
elapsed = time.time() - start_time
103106
logger.info(
104107
f"Response: {request.method} {request.url.path} "
105-
f"-> {response.status_code}"
108+
f"-> {response.status_code} ({elapsed:.3f}s)"
106109
)
107110
return response
108111
except Exception as e:
112+
elapsed = time.time() - start_time
109113
logger.error(
110-
f"Error processing {request.method} {request.url.path}: {e}",
114+
f"Error processing {request.method} {request.url.path} "
115+
f"after {elapsed:.3f}s: {type(e).__name__}: {e}",
111116
exc_info=True,
112117
)
113118
raise

0 commit comments

Comments
 (0)