Skip to content

Commit

Permalink
Started generic exceptions
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed Jul 5, 2024
1 parent 9434771 commit cc9a268
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 59 deletions.
16 changes: 16 additions & 0 deletions packages/api-server/api_server/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AlreadyExistsError(Exception):
"""
Raised when an a resource already exists and there is a conflict.
"""


class NotFoundError(Exception):
"""
Raised when a resource is not found.
"""


class InvalidInputError(Exception):
"""
Raised when an input is invalid.
"""
8 changes: 1 addition & 7 deletions packages/api-server/api_server/repositories/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
from .alerts import (
AlertAlreadyExistsError,
AlertNotFoundError,
AlertRepository,
AlertResponseNotFoundError,
InvalidAlertResponseError,
)
from .alerts import AlertRepository
from .cached_files import CachedFilesRepository, cached_files_repo
from .fleets import FleetRepository
from .rmf import RmfRepository
Expand Down
38 changes: 6 additions & 32 deletions packages/api-server/api_server/repositories/alerts.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
from datetime import datetime
from typing import List

from api_server.exceptions import AlreadyExistsError, InvalidInputError, NotFoundError
from api_server.models import AlertRequest, AlertResponse
from api_server.models import tortoise_models as ttm


class AlertAlreadyExistsError(Exception):
"""
Raised when an alert ID already exists and there is a conflict.
"""


class AlertNotFoundError(Exception):
"""
Raised when an alert is not found.
"""


class InvalidAlertResponseError(Exception):
"""
Raised when a response does not match any of the available responses in the
alert.
"""


class AlertResponseNotFoundError(Exception):
"""
Raised when an alert response is not found.
"""


class AlertRepository:
async def create_new_alert(self, alert: AlertRequest) -> AlertRequest:
exists = await ttm.AlertRequest.exists(id=alert.id)
if exists:
raise AlertAlreadyExistsError(f"Alert with ID {alert.id} already exists")
raise AlreadyExistsError(f"Alert with ID {alert.id} already exists")

await ttm.AlertRequest.create(
id=alert.id,
Expand All @@ -47,19 +23,19 @@ async def create_new_alert(self, alert: AlertRequest) -> AlertRequest:
async def get_alert(self, alert_id: str) -> AlertRequest:
alert = await ttm.AlertRequest.get_or_none(id=alert_id)
if alert is None:
raise AlertNotFoundError(f"Alert with ID {alert_id} does not exists")
raise NotFoundError(f"Alert with ID {alert_id} does not exists")

alert_model = AlertRequest.from_tortoise(alert)
return alert_model

async def create_response(self, alert_id: str, response: str) -> AlertResponse:
alert = await ttm.AlertRequest.get_or_none(id=alert_id)
if alert is None:
raise AlertNotFoundError(f"Alert with ID {alert_id} does not exists")
raise NotFoundError(f"Alert with ID {alert_id} does not exists")

alert_model = AlertRequest.from_tortoise(alert)
if response not in alert_model.responses_available:
raise InvalidAlertResponseError(
raise InvalidInputError(
f"Response [{response}] is not a response option of alert with ID {alert_model.id}"
)

Expand All @@ -76,9 +52,7 @@ async def create_response(self, alert_id: str, response: str) -> AlertResponse:
async def get_alert_response(self, alert_id: str) -> AlertResponse:
response = await ttm.AlertResponse.get_or_none(id=alert_id)
if response is None:
raise AlertResponseNotFoundError(
f"Response to alert with ID {alert_id} does not exists"
)
raise NotFoundError(f"Response to alert with ID {alert_id} does not exists")

response_model = AlertResponse.from_tortoise(response)
return response_model
Expand Down
19 changes: 7 additions & 12 deletions packages/api-server/api_server/routes/alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
from fastapi import Depends, HTTPException
from tortoise.exceptions import IntegrityError

from api_server.exceptions import AlreadyExistsError, InvalidInputError, NotFoundError
from api_server.fast_io import FastIORouter, SubscriptionRequest
from api_server.gateway import rmf_gateway
from api_server.models import AlertRequest, AlertResponse
from api_server.repositories import (
AlertAlreadyExistsError,
AlertNotFoundError,
AlertRepository,
AlertResponseNotFoundError,
InvalidAlertResponseError,
)
from api_server.repositories import AlertRepository
from api_server.rmf_io import alert_events

router = FastIORouter(tags=["Alerts"])
Expand All @@ -34,7 +29,7 @@ async def create_new_alert(
created_alert = await repo.create_new_alert(alert)
except IntegrityError as e:
raise HTTPException(400, e) from e
except AlertAlreadyExistsError as e:
except AlreadyExistsError as e:
raise HTTPException(409, str(e)) from e

alert_events.alert_requests.on_next(created_alert)
Expand All @@ -48,7 +43,7 @@ async def get_alert(alert_id: str, repo: AlertRepository = Depends(AlertReposito
"""
try:
alert_model = await repo.get_alert(alert_id)
except AlertNotFoundError as e:
except NotFoundError as e:
raise HTTPException(404, str(e)) from e

return alert_model
Expand All @@ -73,9 +68,9 @@ async def respond_to_alert(
alert_response_model = await repo.create_response(alert_id, response)
except IntegrityError as e:
raise HTTPException(400, e) from e
except AlertNotFoundError as e:
except NotFoundError as e:
raise HTTPException(404, str(e)) from e
except InvalidAlertResponseError as e:
except InvalidInputError as e:
raise HTTPException(400, str(e)) from e

alert_events.alert_responses.on_next(alert_response_model)
Expand All @@ -92,7 +87,7 @@ async def get_alert_response(
"""
try:
response_model = await repo.get_alert_response(alert_id)
except AlertResponseNotFoundError as e:
except NotFoundError as e:
raise HTTPException(404, str(e)) from e

return response_model
Expand Down
12 changes: 4 additions & 8 deletions packages/api-server/api_server/routes/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@

from api_server import models as mdl
from api_server.app_config import app_config
from api_server.exceptions import AlreadyExistsError
from api_server.logging import LoggerAdapter, get_logger
from api_server.repositories import (
AlertAlreadyExistsError,
AlertRepository,
FleetRepository,
TaskRepository,
)
from api_server.repositories import AlertRepository, FleetRepository, TaskRepository
from api_server.rmf_io import alert_events, fleet_events, task_events

router = APIRouter(tags=["_internal"])
Expand Down Expand Up @@ -93,7 +89,7 @@ async def process_msg(
)
try:
created_alert = await alert_repo.create_new_alert(alert_request)
except AlertAlreadyExistsError as e:
except AlreadyExistsError as e:
logger.error(e)
return
alert_events.alert_requests.on_next(created_alert)
Expand Down Expand Up @@ -122,7 +118,7 @@ async def process_msg(
)
try:
created_alert = await alert_repo.create_new_alert(alert_request)
except AlertAlreadyExistsError as e:
except AlreadyExistsError as e:
logger.error(e)
return
alert_events.alert_requests.on_next(created_alert)
Expand Down

0 comments on commit cc9a268

Please sign in to comment.