diff --git a/services/api-server/src/simcore_service_api_server/api/errors/log_handling_error.py b/services/api-server/src/simcore_service_api_server/api/errors/log_handling_error.py index 3c4c5bbb467..c18ab81c35d 100644 --- a/services/api-server/src/simcore_service_api_server/api/errors/log_handling_error.py +++ b/services/api-server/src/simcore_service_api_server/api/errors/log_handling_error.py @@ -3,21 +3,21 @@ from starlette.responses import JSONResponse from ...services.log_streaming import ( - LogDistributionBaseException, - LogStreamerNotRegistered, - LogStreamerRegistionConflict, + LogDistributionBaseError, + LogStreamerNotRegisteredError, + LogStreamerRegistionConflictError, ) from .http_error import create_error_json_response async def log_handling_error_handler( - _: Request, exc: LogDistributionBaseException + _: Request, exc: LogDistributionBaseError ) -> JSONResponse: msg = f"{exc}" status_code: int = 500 - if isinstance(exc, LogStreamerNotRegistered): + if isinstance(exc, LogStreamerNotRegisteredError): status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - elif isinstance(exc, LogStreamerRegistionConflict): + elif isinstance(exc, LogStreamerRegistionConflictError): status_code = status.HTTP_409_CONFLICT return create_error_json_response(msg, status_code=status_code) diff --git a/services/api-server/src/simcore_service_api_server/core/application.py b/services/api-server/src/simcore_service_api_server/core/application.py index 581735cf54e..675341d00a5 100644 --- a/services/api-server/src/simcore_service_api_server/core/application.py +++ b/services/api-server/src/simcore_service_api_server/core/application.py @@ -12,9 +12,7 @@ from simcore_service_api_server.api.errors.log_handling_error import ( log_handling_error_handler, ) -from simcore_service_api_server.services.log_streaming import ( - LogDistributionBaseException, -) +from simcore_service_api_server.services.log_streaming import LogDistributionBaseError from starlette import status from starlette.exceptions import HTTPException @@ -108,7 +106,7 @@ def init_app(settings: ApplicationSettings | None = None) -> FastAPI: app.add_exception_handler(HTTPException, http_error_handler) app.add_exception_handler(RequestValidationError, http422_error_handler) app.add_exception_handler(HTTPStatusError, httpx_client_error_handler) - app.add_exception_handler(LogDistributionBaseException, log_handling_error_handler) + app.add_exception_handler(LogDistributionBaseError, log_handling_error_handler) app.add_exception_handler(CustomBaseError, custom_error_handler) # SEE https://docs.python.org/3/library/exceptions.html#exception-hierarchy diff --git a/services/api-server/src/simcore_service_api_server/services/log_streaming.py b/services/api-server/src/simcore_service_api_server/services/log_streaming.py index acc8bdf075c..3ef2fef416c 100644 --- a/services/api-server/src/simcore_service_api_server/services/log_streaming.py +++ b/services/api-server/src/simcore_service_api_server/services/log_streaming.py @@ -16,15 +16,15 @@ _SLEEP_SECONDS_BEFORE_CHECK_JOB_STATUS: Final[PositiveInt] = 10 -class LogDistributionBaseException(Exception): +class LogDistributionBaseError(Exception): pass -class LogStreamerNotRegistered(LogDistributionBaseException): +class LogStreamerNotRegisteredError(LogDistributionBaseError): pass -class LogStreamerRegistionConflict(LogDistributionBaseException): +class LogStreamerRegistionConflictError(LogDistributionBaseError): pass @@ -63,7 +63,7 @@ async def _distribute_logs(self, data: bytes): callback = self._log_streamers.get(item.job_id) if callback is None: msg = f"Could not forward log because a logstreamer associated with job_id={item.job_id} was not registered" - raise LogStreamerNotRegistered(msg) + raise LogStreamerNotRegisteredError(msg) await callback(item) return True @@ -72,7 +72,7 @@ async def register( ): if job_id in self._log_streamers: msg = f"A stream was already connected to {job_id=}. Only a single stream can be connected at the time" - raise LogStreamerRegistionConflict(msg) + raise LogStreamerRegistionConflictError(msg) self._log_streamers[job_id] = callback await self._rabbit_client.add_topics( LoggerRabbitMessage.get_channel_name(), topics=[f"{job_id}.*"] @@ -81,7 +81,7 @@ async def register( async def deregister(self, job_id: JobID): if job_id not in self._log_streamers: msg = f"No stream was connected to {job_id=}." - raise LogStreamerNotRegistered(msg) + raise LogStreamerNotRegisteredError(msg) await self._rabbit_client.remove_topics( LoggerRabbitMessage.get_channel_name(), topics=[f"{job_id}.*"] ) @@ -128,7 +128,7 @@ async def _project_done(self) -> bool: async def log_generator(self) -> AsyncIterable[str]: if not self._is_registered: msg = f"LogStreamer for job_id={self._job_id} is not correctly registered" - raise LogStreamerNotRegistered(msg) + raise LogStreamerNotRegisteredError(msg) last_log_time: datetime | None = None while True: while self._queue.empty(): diff --git a/services/api-server/tests/unit/test_services_rabbitmq.py b/services/api-server/tests/unit/test_services_rabbitmq.py index c830db93705..8b81c95acb7 100644 --- a/services/api-server/tests/unit/test_services_rabbitmq.py +++ b/services/api-server/tests/unit/test_services_rabbitmq.py @@ -41,8 +41,8 @@ from simcore_service_api_server.services.log_streaming import ( LogDistributor, LogStreamer, - LogStreamerNotRegistered, - LogStreamerRegistionConflict, + LogStreamerNotRegisteredError, + LogStreamerRegistionConflictError, ) pytest_simcore_core_services_selection = [ @@ -217,7 +217,7 @@ async def _(job_log: JobLog): pass await log_distributor.register(project_id, _) - with pytest.raises(LogStreamerRegistionConflict): + with pytest.raises(LogStreamerRegistionConflictError): await log_distributor.register(project_id, _) await log_distributor.deregister(project_id) @@ -413,6 +413,6 @@ async def test_log_generator(mocker: MockFixture, faker: Faker): async def test_log_generator_context(mocker: MockFixture, faker: Faker): log_streamer = LogStreamer(user_id=3, director2_api=None, job_id=None, log_distributor=None, max_log_check_seconds=1) # type: ignore - with pytest.raises(LogStreamerNotRegistered): + with pytest.raises(LogStreamerNotRegisteredError): async for log in log_streamer.log_generator(): print(log)