diff --git a/ldap_jwt_auth/core/exceptions.py b/ldap_jwt_auth/core/exceptions.py index f81741f..1b1a778 100644 --- a/ldap_jwt_auth/core/exceptions.py +++ b/ldap_jwt_auth/core/exceptions.py @@ -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. """ diff --git a/ldap_jwt_auth/core/maintenance.py b/ldap_jwt_auth/core/maintenance.py index e670f53..dd29340 100644 --- a/ldap_jwt_auth/core/maintenance.py +++ b/ldap_jwt_auth/core/maintenance.py @@ -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. @@ -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 diff --git a/ldap_jwt_auth/routers/maintenance.py b/ldap_jwt_auth/routers/maintenance.py index 2601369..162fdc0 100644 --- a/ldap_jwt_auth/routers/maintenance.py +++ b/ldap_jwt_auth/routers/maintenance.py @@ -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 @@ -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( @@ -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 diff --git a/test/unit/maintenance/test_maintenance.py b/test/unit/maintenance/test_maintenance.py index adb45cb..1d3cf57 100644 --- a/test/unit/maintenance/test_maintenance.py +++ b/test/unit/maintenance/test_maintenance.py @@ -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 @@ -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" @@ -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"