Skip to content

Commit

Permalink
refactored exceptions in maintenance class #88
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGuarnaccia5 committed Jul 1, 2024
1 parent e21e09a commit c98e731
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
4 changes: 2 additions & 2 deletions ldap_jwt_auth/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class InvalidMaintenanceFileError(Exception):
"""


class MissingMaintenanceFileError(Exception):
class MaintenanceFileReadError(Exception):
"""
Exception raised when the maintenance state file is missing or its data cannot be read.
Exception raised when the maintenance state file's data cannot be read.
"""
34 changes: 23 additions & 11 deletions ldap_jwt_auth/core/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
"""

import json
import logging

from pydantic import ValidationError
from ldap_jwt_auth.core.config import config
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MaintenanceFileReadError
from ldap_jwt_auth.core.schemas import MaintenanceState, ScheduledMaintenanceState

logger = logging.getLogger()


class Maintenance:
"""
Class for managing maintenance and scheduled maintenance states.
Expand All @@ -18,32 +22,40 @@ def get_maintenance_state(self) -> MaintenanceState:
"""
Return the maintenance state of the system
:return: Maintenance state schema
:return: Maintenance state
:raises InvalidFileFormat: If the maintenance state file is incorrectly formatted
:raises MissingMaintenanceFileError: If the maintenance state file can not be found or read
:raises MaintenanceFileReadError: If the scheduled maintenance state file's data cannot be read
"""
try:
with open(config.maintenance.maintenance_path, "r", encoding='utf-8') as file:
with open(config.maintenance.maintenance_path, "r", encoding="utf-8") as file:
data = json.load(file)
return MaintenanceState(**data)
except (OSError, json.JSONDecodeError, TypeError) as exc:
raise MissingMaintenanceFileError("Unable to find maintenance file") from exc
message = "An error occurred while trying to find and read the maintenance file"
logger.exception(message)
raise MaintenanceFileReadError(message) from exc
except ValidationError as exc:
raise InvalidMaintenanceFileError("Maintenance file format is incorrect") from exc
message = "An error occurred while validating the data in the maintenance file"
logger.exception(message)
raise InvalidMaintenanceFileError(message) from exc

def get_scheduled_maintenance_state(self) -> ScheduledMaintenanceState:
"""
Return the scheduled maintenance state of the system
:return: Scheduled maintenance state schema
:return: Scheduled maintenance state
:raises InvalidFileFormat: If the scheduled maintenance state file is incorrectly formatted
:raises MissingMaintenanceFileError: If the scheduled maintenance state file can not be found or read
:raises MaintenanceFileReadError: If the scheduled maintenance state file's data cannot be read
"""
try:
with open(config.maintenance.scheduled_maintenance_path, "r", encoding='utf-8') as file:
with open(config.maintenance.scheduled_maintenance_path, "r", encoding="utf-8") as file:
data = json.load(file)
return ScheduledMaintenanceState(**data)
except (OSError, json.JSONDecodeError, TypeError) as exc:
raise MissingMaintenanceFileError("Unable to find scheduled maintenance file") from exc
message = "An error occurred while trying to find and read the scheduled maintenance file"
logger.exception(message)
raise MaintenanceFileReadError(message) from exc
except ValidationError as exc:
raise InvalidMaintenanceFileError("Scheduled maintenance file format is incorrect") from exc
message = "An error occurred while validating the data in the scheduled maintenance file"
logger.exception(message)
raise InvalidMaintenanceFileError(message) from exc
20 changes: 5 additions & 15 deletions ldap_jwt_auth/routers/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, status
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MaintenanceFileReadError
from ldap_jwt_auth.core.maintenance import Maintenance
from ldap_jwt_auth.core.schemas import MaintenanceState, ScheduledMaintenanceState

Expand All @@ -24,13 +24,8 @@ def get_maintenance_state(maintenance: Annotated[Maintenance, Depends(Maintenanc

try:
return maintenance.get_maintenance_state()

except MissingMaintenanceFileError as exc:
message = "Unable to find maintenance file"
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc
except InvalidMaintenanceFileError as exc:
message = "Maintenance file format is incorrect"
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc
except (InvalidMaintenanceFileError, MaintenanceFileReadError) as exc:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Something went wrong") from exc


@router.get(
Expand All @@ -45,10 +40,5 @@ def get_scheduled_maintenance_state(
logger.info("Getting scheduled maintenance state")
try:
return maintenance.get_scheduled_maintenance_state()

except MissingMaintenanceFileError as exc:
message = "Unable to find scheduled maintenance file"
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc
except InvalidMaintenanceFileError as exc:
message = "Scheduled maintenance file format is incorrect"
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message) from exc
except (InvalidMaintenanceFileError, MaintenanceFileReadError) as exc:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Something went wrong") from exc
6 changes: 3 additions & 3 deletions test/unit/maintenance/test_maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json

import pytest
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MissingMaintenanceFileError
from ldap_jwt_auth.core.exceptions import InvalidMaintenanceFileError, MaintenanceFileReadError
from ldap_jwt_auth.core.maintenance import Maintenance
from unittest.mock import mock_open, patch

Expand Down Expand Up @@ -50,7 +50,7 @@ def test_get_maintenance_state_missing_file():
mocked_open.side_effect = IOError
maintenance = Maintenance()

with pytest.raises(MissingMaintenanceFileError) as exc:
with pytest.raises(MaintenanceFileReadError) as exc:
maintenance.get_maintenance_state()
assert str(exc.value) == "Unable to find maintenance file"

Expand Down Expand Up @@ -95,6 +95,6 @@ def test_get_scheduled_maintenance_state_missing_file():
mocked_open.side_effect = IOError
maintenance = Maintenance()

with pytest.raises(MissingMaintenanceFileError) as exc:
with pytest.raises(MaintenanceFileReadError) as exc:
maintenance.get_scheduled_maintenance_state()
assert str(exc.value) == "Unable to find scheduled maintenance file"

0 comments on commit c98e731

Please sign in to comment.