Skip to content

Commit ec9ca07

Browse files
committed
feat: aligned error handling for upscale_tasks
1 parent 8b5f292 commit ec9ca07

File tree

4 files changed

+84
-25
lines changed

4 files changed

+84
-25
lines changed

app/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _decode_token(token: str):
4242
except Exception:
4343
raise AuthException(
4444
http_status=status.HTTP_401_UNAUTHORIZED,
45-
message="Could not validate credentials!",
45+
message="Could not validate credentials. Please retry signing in.",
4646
)
4747

4848

app/error.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class JobNotFoundException(DispatcherException):
5656
message: str = "The requested job was not found."
5757

5858

59+
class TaskNotFoundException(DispatcherException):
60+
http_status: int = status.HTTP_404_NOT_FOUND
61+
error_code: str = "TASK_NOT_FOUND"
62+
message: str = "The requested task was not found."
63+
64+
5965
class InternalException(DispatcherException):
6066
http_status: int = status.HTTP_500_INTERNAL_SERVER_ERROR
6167
error_code: str = "INTERNAL_ERROR"

app/routers/jobs_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ async def ws_jobs_status(
121121
)
122122
await websocket.close(code=1008, reason=ae.error_code)
123123
except Exception as e:
124-
logger.error(f"Unexpected error occurred during websocket authentication: {e}")
124+
logger.error(f"Unexpected error occurred during websocket : {e}")
125125
await WSStatusMessage(
126126
type="error",
127-
message="Something went wrong during authentication. Please try again.",
127+
message="An error occurred while monitoring the job status.",
128128
).model_dump()
129129
await websocket.close(code=1008, reason="INTERNAL_ERROR")
130130
finally:

app/routers/upscale_tasks.py

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
from app.auth import oauth2_scheme, websocket_authenticate
1818
from app.database.db import SessionLocal, get_db
19+
from app.error import (
20+
DispatcherException,
21+
ErrorResponse,
22+
InternalException,
23+
TaskNotFoundException,
24+
)
25+
from app.middleware.error_handling import get_dispatcher_error_response
1926
from app.schemas.enum import OutputFormatEnum, ProcessTypeEnum
2027
from app.schemas.unit_job import (
2128
ServiceDetails,
@@ -43,6 +50,19 @@
4350
status_code=status.HTTP_201_CREATED,
4451
tags=["Upscale Tasks"],
4552
summary="Create a new upscaling task",
53+
responses={
54+
InternalException.http_status: {
55+
"description": "Internal server error",
56+
"model": ErrorResponse,
57+
"content": {
58+
"application/json": {
59+
"example": get_dispatcher_error_response(
60+
InternalException(), "request-id"
61+
)
62+
}
63+
},
64+
},
65+
},
4666
)
4767
async def create_upscale_task(
4868
payload: Annotated[
@@ -120,20 +140,42 @@ async def create_upscale_task(
120140
upscaling_task_id=task.id,
121141
)
122142
return task
123-
except HTTPException as e:
124-
raise e
143+
except DispatcherException as de:
144+
raise de
125145
except Exception as e:
126-
logger.exception(f"Error creating upscale task: {e}")
127-
raise HTTPException(
128-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
129-
detail=f"An error occurred while creating the upscale task: {e}",
146+
logger.error(f"Error getting creating upscaling task: {e}")
147+
raise InternalException(
148+
message="An error occurred while retrieving processing job results."
130149
)
131150

132151

133152
@router.get(
134153
"/upscale_tasks/{task_id}",
135154
tags=["Upscale Tasks"],
136-
responses={404: {"description": "Upscale task not found"}},
155+
responses={
156+
TaskNotFoundException.http_status: {
157+
"description": "Upscaling not found",
158+
"model": ErrorResponse,
159+
"content": {
160+
"application/json": {
161+
"example": get_dispatcher_error_response(
162+
TaskNotFoundException(), "request-id"
163+
)
164+
}
165+
},
166+
},
167+
InternalException.http_status: {
168+
"description": "Internal server error",
169+
"model": ErrorResponse,
170+
"content": {
171+
"application/json": {
172+
"example": get_dispatcher_error_response(
173+
InternalException(), "request-id"
174+
)
175+
}
176+
},
177+
},
178+
},
137179
)
138180
async def get_upscale_task(
139181
task_id: int,
@@ -144,24 +186,18 @@ async def get_upscale_task(
144186
job = await get_upscaling_task_by_user_id(token, db, task_id)
145187
if not job:
146188
logger.error(f"Upscale task {task_id} not found")
147-
raise HTTPException(
148-
status_code=404,
149-
detail=f"Upscale task {task_id} not found",
150-
)
189+
raise TaskNotFoundException()
151190
return job
152-
except HTTPException as e:
153-
raise e
191+
except DispatcherException as de:
192+
raise de
154193
except Exception as e:
155-
logger.exception(f"Error retrieving upscale task {task_id}: {e}")
156-
raise HTTPException(
157-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
158-
detail=f"An error occurred while retrieving the upscale task {task_id}: {e}",
194+
logger.error(f"Error retrieving upscale task {task_id}: {e}")
195+
raise InternalException(
196+
message="An error occurred while retrieving the upscale task."
159197
)
160198

161199

162-
@router.websocket(
163-
"/ws/upscale_tasks/{task_id}",
164-
)
200+
@router.websocket("/ws/upscale_tasks/{task_id}")
165201
async def ws_task_status(
166202
websocket: WebSocket,
167203
task_id: int,
@@ -212,6 +248,23 @@ async def ws_task_status(
212248

213249
except WebSocketDisconnect:
214250
logger.info("WebSocket disconnected")
251+
except DispatcherException as ae:
252+
logger.error(f"Dispatcher exception detected: {ae.message}")
253+
await websocket.send_json(
254+
WSTaskStatusMessage(
255+
type="error", task_id=task_id, message=ae.message
256+
).model_dump()
257+
)
258+
await websocket.close(code=1008, reason=ae.error_code)
215259
except Exception as e:
216-
logger.exception(f"Error in upscaling task status websocket: {e}")
217-
await websocket.close(code=1011, reason=f"Error in job status websocket: {e}")
260+
logger.error(
261+
f"An error occurred while monitoring upscaling task {task_id}: {e}"
262+
)
263+
await WSTaskStatusMessage(
264+
type="error",
265+
task_id=task_id,
266+
message="An error occurred while monitoring upscaling task.",
267+
).model_dump()
268+
await websocket.close(code=1008, reason="INTERNAL_ERROR")
269+
finally:
270+
db.close()

0 commit comments

Comments
 (0)