Skip to content

Commit 8d333b0

Browse files
fix: add exception handler
1 parent 01a2883 commit 8d333b0

File tree

2 files changed

+20
-21
lines changed
  • services/api-server/src/simcore_service_api_server

2 files changed

+20
-21
lines changed

services/api-server/src/simcore_service_api_server/api/routes/tasks.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
from typing import Annotated, Any
33

4-
from celery_library.errors import TaskNotFoundError
54
from common_library.error_codes import create_error_code
65
from fastapi import APIRouter, Depends, FastAPI, HTTPException, status
76
from models_library.api_schemas_long_running_tasks.base import TaskProgress
@@ -110,16 +109,10 @@ async def get_task_status(
110109
):
111110
task_manager = get_task_manager(app)
112111

113-
try:
114-
task_status = await task_manager.get_task_status(
115-
task_filter=_get_task_filter(user_id, product_name),
116-
task_uuid=TaskUUID(f"{task_id}"),
117-
)
118-
except TaskNotFoundError as err:
119-
raise HTTPException(
120-
status_code=status.HTTP_404_NOT_FOUND,
121-
detail="Task not found",
122-
) from err
112+
task_status = await task_manager.get_task_status(
113+
task_filter=_get_task_filter(user_id, product_name),
114+
task_uuid=TaskUUID(f"{task_id}"),
115+
)
123116

124117
return TaskStatus(
125118
task_progress=TaskProgress(
@@ -184,16 +177,10 @@ async def get_task_result(
184177
task_manager = get_task_manager(app)
185178
task_filter = _get_task_filter(user_id, product_name)
186179

187-
try:
188-
task_status = await task_manager.get_task_status(
189-
task_filter=task_filter,
190-
task_uuid=TaskUUID(f"{task_id}"),
191-
)
192-
except TaskNotFoundError as err:
193-
raise HTTPException(
194-
status_code=status.HTTP_404_NOT_FOUND,
195-
detail="Task not found",
196-
) from err
180+
task_status = await task_manager.get_task_status(
181+
task_filter=task_filter,
182+
task_uuid=TaskUUID(f"{task_id}"),
183+
)
197184

198185
if not task_status.is_done:
199186
raise HTTPException(

services/api-server/src/simcore_service_api_server/exceptions/handlers/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from os import error
2+
13
from celery.exceptions import ( # type: ignore[import-untyped] #pylint: disable=no-name-in-module
24
CeleryError,
35
)
6+
from celery_library.errors import TaskNotFoundError
47
from fastapi import FastAPI
58
from fastapi.exceptions import RequestValidationError
69
from httpx import HTTPError as HttpxException
@@ -41,6 +44,15 @@ def setup(app: FastAPI, *, is_debug: bool = False):
4144
),
4245
)
4346

47+
app.add_exception_handler(
48+
TaskNotFoundError,
49+
make_handler_for_exception(
50+
TaskNotFoundError,
51+
status.HTTP_404_NOT_FOUND,
52+
error_message="The requested task was not found",
53+
),
54+
)
55+
4456
app.add_exception_handler(
4557
CeleryError,
4658
make_handler_for_exception(

0 commit comments

Comments
 (0)