Skip to content

Commit 2ba957c

Browse files
authored
feat: enhance error handling and logging across various routers and services (#68)
- Added print statements to log errors during user creation, authentication, pad management, and template pad operations, improving visibility into issues. - Updated function names in pad router to reflect "pad" terminology instead of "canvas," enhancing clarity in the API. - Ensured consistent error messages for HTTP exceptions across different modules, aiding in debugging and user feedback.
1 parent b88811b commit 2ba957c

File tree

7 files changed

+29
-5
lines changed

7 files changed

+29
-5
lines changed

src/backend/database/service/user_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async def sync_user_with_token_data(self, user_id: UUID, token_data: Dict[str, A
128128
roles=token_data.get("roles", [])
129129
)
130130
except ValueError as e:
131+
print(f"Error creating user: {e}")
131132
# Handle case where user might have been created in a race condition
132133
if "already exists" in str(e):
133134
user_data = await self.get_user(user_id)

src/backend/dependencies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def _handle_auth_error(self, detail: str, status_code: int = 401) -> Optional[No
137137
"""Handle authentication errors based on auto_error setting"""
138138
if self.auto_error:
139139
headers = {"WWW-Authenticate": "Bearer"} if status_code == 401 else None
140+
print(f"Authentication error: {detail}")
140141
raise HTTPException(
141142
status_code=status_code,
142143
detail=detail,

src/backend/routers/auth_router.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ async def callback(
4040
):
4141
session_id = request.cookies.get('session_id')
4242
if not session_id:
43+
print("No session ID found")
4344
raise HTTPException(status_code=400, detail="No session")
4445

4546
# Exchange code for token

src/backend/routers/pad_router.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212

1313
@pad_router.post("")
14-
async def save_canvas(
14+
async def save_pad(
1515
data: Dict[str, Any],
1616
user: UserSession = Depends(require_auth),
1717
pad_service: PadService = Depends(get_pad_service),
1818
backup_service: BackupService = Depends(get_backup_service),
1919
):
20-
"""Save canvas data for the authenticated user"""
20+
"""Save pad data for the authenticated user"""
2121
try:
2222
# Check if user already has a pad
2323
user_pads = await pad_service.get_pads_by_owner(user.id)
@@ -44,17 +44,18 @@ async def save_canvas(
4444

4545
return {"status": "success"}
4646
except Exception as e:
47+
print(f"Error saving pad data: {str(e)}")
4748
raise HTTPException(status_code=500, detail=f"Failed to save canvas data: {str(e)}")
4849

4950

5051
@pad_router.get("")
51-
async def get_canvas(
52+
async def get_pad(
5253
user: UserSession = Depends(require_auth),
5354
pad_service: PadService = Depends(get_pad_service),
5455
template_pad_service: TemplatePadService = Depends(get_template_pad_service),
5556
backup_service: BackupService = Depends(get_backup_service)
5657
):
57-
"""Get canvas data for the authenticated user"""
58+
"""Get pad data for the authenticated user"""
5859
try:
5960
# Get user's pads
6061
user_pads = await pad_service.get_pads_by_owner(user.id)
@@ -73,7 +74,8 @@ async def get_canvas(
7374
# Return the first pad's data (assuming one pad per user for now)
7475
return user_pads[0]["data"]
7576
except Exception as e:
76-
raise HTTPException(status_code=500, detail=f"Failed to get canvas data: {str(e)}")
77+
print(f"Error getting pad data: {str(e)}")
78+
raise HTTPException(status_code=500, detail=f"Failed to get pad data: {str(e)}")
7779

7880

7981
@pad_router.post("/from-template/{name}")
@@ -110,8 +112,10 @@ async def create_pad_from_template(
110112

111113
return pad
112114
except ValueError as e:
115+
print(f"Error creating pad from template: {str(e)}")
113116
raise HTTPException(status_code=400, detail=str(e))
114117
except Exception as e:
118+
print(f"Error creating pad from template: {str(e)}")
115119
raise HTTPException(status_code=500, detail=f"Failed to create pad from template: {str(e)}")
116120

117121

@@ -141,4 +145,5 @@ async def get_recent_canvas_backups(
141145

142146
return {"backups": backups}
143147
except Exception as e:
148+
print(f"Error getting canvas backups: {str(e)}")
144149
raise HTTPException(status_code=500, detail=f"Failed to get canvas backups: {str(e)}")

src/backend/routers/template_pad_router.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async def create_template_pad(
2626
)
2727
return template_pad
2828
except ValueError as e:
29+
print(f"Error creating template pad: {str(e)}")
2930
raise HTTPException(status_code=400, detail=str(e))
3031

3132

@@ -39,6 +40,7 @@ async def get_all_template_pads(
3940
template_pads = await template_pad_service.get_all_templates()
4041
return template_pads
4142
except Exception as e:
43+
print(f"Error getting template pads: {str(e)}")
4244
raise HTTPException(status_code=500, detail=f"Failed to get template pads: {str(e)}")
4345

4446

@@ -51,6 +53,7 @@ async def get_template_pad(
5153
"""Get a specific template pad by name"""
5254
template_pad = await template_pad_service.get_template_by_name(name)
5355
if not template_pad:
56+
print(f"Template pad not found: {name}")
5457
raise HTTPException(status_code=404, detail="Template pad not found")
5558

5659
return template_pad
@@ -67,10 +70,12 @@ async def update_template_pad(
6770
try:
6871
updated_template = await template_pad_service.update_template(name, data)
6972
if not updated_template:
73+
print(f"Template pad not found: {name}")
7074
raise HTTPException(status_code=404, detail="Template pad not found")
7175

7276
return updated_template
7377
except ValueError as e:
78+
print(f"Error updating template pad: {str(e)}")
7479
raise HTTPException(status_code=400, detail=str(e))
7580

7681

@@ -85,10 +90,12 @@ async def update_template_pad_data(
8590
try:
8691
updated_template = await template_pad_service.update_template_data(name, data)
8792
if not updated_template:
93+
print(f"Template pad not found: {name}")
8894
raise HTTPException(status_code=404, detail="Template pad not found")
8995

9096
return updated_template
9197
except ValueError as e:
98+
print(f"Error updating template pad data: {str(e)}")
9299
raise HTTPException(status_code=400, detail=str(e))
93100

94101

@@ -102,8 +109,10 @@ async def delete_template_pad(
102109
try:
103110
success = await template_pad_service.delete_template(name)
104111
if not success:
112+
print(f"Template pad not found: {name}")
105113
raise HTTPException(status_code=404, detail="Template pad not found")
106114

107115
return {"status": "success", "message": "Template pad deleted successfully"}
108116
except ValueError as e:
117+
print(f"Error deleting template pad: {str(e)}")
109118
raise HTTPException(status_code=400, detail=str(e))

src/backend/routers/user_router.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async def create_user(
3939
)
4040
return user
4141
except ValueError as e:
42+
print(f"Error creating user: {str(e)}")
4243
raise HTTPException(status_code=400, detail=str(e))
4344

4445

@@ -74,6 +75,7 @@ async def get_user_info(
7475
# Sync user with token data
7576
user_data = await user_service.sync_user_with_token_data(user.id, token_data)
7677
except Exception as e:
78+
print(f"Error syncing user data: {str(e)}")
7779
raise HTTPException(
7880
status_code=500,
7981
detail=f"Error syncing user data: {e}"

src/backend/routers/workspace_router.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ async def get_workspace_state(
2828

2929
coder_user: dict = coder_api.get_user_by_email(user.email)
3030
if not coder_user:
31+
print(f"Coder user not found for user {user.email} ({user.id})")
3132
raise HTTPException(status_code=404, detail=f"Coder user not found for user {user.email} ({user.id})")
3233

3334
coder_username: str = coder_user.get('username', None)
3435
if not coder_username:
36+
print(f"Coder username not found for user {user.email} ({user.id})")
3537
raise HTTPException(status_code=404, detail=f"Coder username not found for user {user.email} ({user.id})")
3638

3739
workspace: dict = coder_api.get_workspace_status_for_user(coder_username)
3840
if not workspace:
41+
print(f"Coder workspace not found for user {user.email} ({user.id})")
3942
raise HTTPException(status_code=404, detail=f"Coder Workspace not found for user {user.email} ({user.id})")
4043

4144
latest_build: dict = workspace.get('latest_build', {})
@@ -68,6 +71,7 @@ async def start_workspace(
6871
response = coder_api.start_workspace(workspace.id)
6972
return JSONResponse(content=response)
7073
except Exception as e:
74+
print(f"Error starting workspace: {str(e)}")
7175
raise HTTPException(status_code=500, detail=str(e))
7276

7377

@@ -86,4 +90,5 @@ async def stop_workspace(
8690
response = coder_api.stop_workspace(workspace.id)
8791
return JSONResponse(content=response)
8892
except Exception as e:
93+
print(f"Error stopping workspace: {str(e)}")
8994
raise HTTPException(status_code=500, detail=str(e))

0 commit comments

Comments
 (0)