22import logging
33
44from aiohttp import web
5+ from common_library .error_codes import create_error_code
56from common_library .json_serialization import json_dumps
7+ from common_library .user_messages import user_message
68from models_library .api_schemas_catalog .service_access_rights import (
79 ServiceAccessRightsGet ,
810)
2325 NodeServiceGet ,
2426 ProjectNodeServicesGet ,
2527)
28+ from models_library .basic_types import IDStr
2629from models_library .groups import EVERYONE_GROUP_ID , Group , GroupID , GroupType
2730from models_library .projects import Project , ProjectID
2831from models_library .projects_nodes_io import NodeID , NodeIDStr
32+ from models_library .rest_error import ErrorGet
2933from models_library .services import ServiceKeyVersion
3034from models_library .services_resources import ServiceResourcesDict
3135from models_library .services_types import ServiceKey , ServiceVersion
4347 UNDEFINED_DEFAULT_SIMCORE_USER_AGENT_VALUE ,
4448 X_SIMCORE_USER_AGENT ,
4549)
50+ from servicelib .logging_errors import create_troubleshootting_log_kwargs
4651from servicelib .long_running_tasks .models import TaskProgress
4752from servicelib .long_running_tasks .task import TaskRegistry
4853from servicelib .mimetype_constants import MIMETYPE_APPLICATION_JSON
5762from ..._meta import API_VTAG as VTAG
5863from ...catalog import catalog_service
5964from ...dynamic_scheduler import api as dynamic_scheduler_service
65+ from ...exception_handling import create_error_response
6066from ...groups import api as groups_service
6167from ...groups .exceptions import GroupNotFoundError
6268from ...login .decorators import login_required
6975from .._nodes_service import NodeScreenshot , get_node_screenshots
7076from ..api import has_user_project_access_rights
7177from ..exceptions import (
72- BaseProjectError ,
7378 NodeNotFoundError ,
7479 ProjectNodeResourcesInsufficientRightsError ,
7580 ProjectNodeResourcesInvalidError ,
@@ -300,16 +305,12 @@ async def start_node(request: web.Request) -> web.Response:
300305 return web .json_response (status = status .HTTP_204_NO_CONTENT )
301306
302307
303- class StopDynamicServiceTaskError (BaseProjectError ):
304- msg_template = "Could not stop dynamic service {project_id}.{node_id}: {cause}"
305-
306-
307308async def _stop_dynamic_service_task (
308309 progress : TaskProgress ,
309310 * ,
310311 app : web .Application ,
311312 dynamic_service_stop : DynamicServiceStop ,
312- ):
313+ ) -> web . Response :
313314 _ = progress
314315 # NOTE: _handle_project_nodes_exceptions only decorate handlers
315316 try :
@@ -328,27 +329,41 @@ async def _stop_dynamic_service_task(
328329 return web .json_response (status = status .HTTP_204_NO_CONTENT )
329330
330331 except (RPCServerError , ServiceWaitingForManualInterventionError ) as exc :
331-
332- raise StopDynamicServiceTaskError (
333- project_id = dynamic_service_stop .project_id ,
334- node_id = dynamic_service_stop .node_id ,
335- user_id = dynamic_service_stop .user_id ,
336- save_state = dynamic_service_stop .save_state ,
337- simcore_user_agent = dynamic_service_stop .simcore_user_agent ,
338- cause = str (exc ),
339- ) from exc
332+ error_code = getattr (exc , "error_code" , None ) or create_error_code (exc )
333+ user_error_msg = user_message (
334+ f"Could not stop dynamic service { dynamic_service_stop .project_id } .{ dynamic_service_stop .node_id } "
335+ )
336+ _logger .exception (
337+ ** create_troubleshootting_log_kwargs (
338+ user_error_msg ,
339+ error = exc ,
340+ error_code = error_code ,
341+ error_context = {
342+ "project_id" : dynamic_service_stop .project_id ,
343+ "node_id" : dynamic_service_stop .node_id ,
344+ "user_id" : dynamic_service_stop .user_id ,
345+ "save_state" : dynamic_service_stop .save_state ,
346+ "simcore_user_agent" : dynamic_service_stop .simcore_user_agent ,
347+ },
348+ )
349+ )
350+ # ANE: in case there is an error reply as not found
351+ return create_error_response (
352+ error = ErrorGet (
353+ message = user_error_msg ,
354+ support_id = IDStr (error_code ),
355+ status = status .HTTP_404_NOT_FOUND ,
356+ ),
357+ status_code = status .HTTP_404_NOT_FOUND ,
358+ )
340359
341360 except ServiceWasNotFoundError :
342361 # in case the service is not found reply as all OK
343362 return web .json_response (status = status .HTTP_204_NO_CONTENT )
344363
345364
346365def register_stop_dynamic_service_task (app : web .Application ) -> None :
347- TaskRegistry .register (
348- _stop_dynamic_service_task ,
349- allowed_errors = (StopDynamicServiceTaskError ,),
350- app = app ,
351- )
366+ TaskRegistry .register (_stop_dynamic_service_task , app = app )
352367
353368
354369@routes .post (
0 commit comments