Skip to content

Commit

Permalink
ruff: exceptions naming
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Jan 28, 2024
1 parent e394ed3 commit 68e520f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

Expand All @@ -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}.*"]
Expand All @@ -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}.*"]
)
Expand Down Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions services/api-server/tests/unit/test_services_rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
from simcore_service_api_server.services.log_streaming import (
LogDistributor,
LogStreamer,
LogStreamerNotRegistered,
LogStreamerRegistionConflict,
LogStreamerNotRegisteredError,
LogStreamerRegistionConflictError,
)

pytest_simcore_core_services_selection = [
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

0 comments on commit 68e520f

Please sign in to comment.