1616
1717from app .auth import oauth2_scheme , websocket_authenticate
1818from 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
1926from app .schemas .enum import OutputFormatEnum , ProcessTypeEnum
2027from app .schemas .unit_job import (
2128 ServiceDetails ,
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)
4767async 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)
138180async 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}" )
165201async 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