Skip to content

Commit

Permalink
Use service in exceptions & inherit from same base exception
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyBA committed Nov 27, 2024
1 parent 8e096db commit 253f750
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions notifications_api_common/management/commands/register_kanalen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from requests import Response
from requests.exceptions import JSONDecodeError, RequestException
from zgw_consumers.models import Service

from ...kanalen import KANAAL_REGISTRY
from ...models import NotificationsConfig
Expand All @@ -15,32 +16,39 @@
logger = logging.getLogger(__name__)


class KanaalExists(Exception):
pass


class KanaalException(Exception):
kanaal: str
data: dict | list
service: Service

def __init__(self, kanaal: str, data: Optional[dict | list] = None):
def __init__(
self, kanaal: str, service: Service, data: Optional[dict | list] = None
):
super().__init__()

self.kanaal = kanaal
self.service = service
self.data = data or {}


class KanaalRequestException(KanaalException):
def __str__(self) -> str:
return f"Unable to retrieve kanaal {self.kanaal}: {self.data}"
return (
f"Unable to retrieve kanaal {self.kanaal} from {self.service}: {self.data}"
)


class KanaalCreateException(KanaalException):
def __str__(self) -> str:
return f"Unable to create kanaal {self.kanaal}: {self.data}"
return f"Unable to create kanaal {self.kanaal} at {self.service}: {self.data}"


class KanaalExistsException(KanaalException):
def __str__(self) -> str:
return f"Kanaal '{self.kanaal}' already exists within {self.service}"


def create_kanaal(kanaal: str) -> None:
def create_kanaal(kanaal: str, service: Service) -> None:
"""
Create a kanaal, if it doesn't exist yet.
"""
Expand All @@ -58,10 +66,12 @@ def create_kanaal(kanaal: str) -> None:
kanalen: list[dict] = response.json() or []
response.raise_for_status()
except (RequestException, JSONDecodeError) as exception:
raise KanaalRequestException(kanaal=kanaal, data=response_data) from exception
raise KanaalRequestException(
kanaal=kanaal, service=service, data=response_data
) from exception

if kanalen:
raise KanaalExists()
raise KanaalExistsException(kanaal=kanaal, service=service, data=response_data)

# build up own documentation URL
domain = Site.objects.get_current().domain
Expand All @@ -83,7 +93,9 @@ def create_kanaal(kanaal: str) -> None:
response_data: dict = response.json() or {}
response.raise_for_status()
except (RequestException, JSONDecodeError) as exception:
raise KanaalCreateException(kanaal=kanaal, data=response_data) from exception
raise KanaalCreateException(
kanaal=kanaal, service=service, data=response_data
) from exception


class Command(BaseCommand):
Expand All @@ -108,7 +120,7 @@ def handle(self, **options):
"`notifications_api_service` configured"
)

api_root = config.notifications_api_service.api_root
service = config.notifications_api_service

# use CLI arg or fall back to setting
kanalen = options["kanalen"] or sorted(
Expand All @@ -117,9 +129,13 @@ def handle(self, **options):

for kanaal in kanalen:
try:
create_kanaal(kanaal)
self.stdout.write(f"Registered kanaal '{kanaal}' with {api_root}")
except KanaalExists:
self.stderr.write(f"Kanaal '{kanaal}' already exists within {api_root}")
except (KanaalRequestException, KanaalCreateException) as exception:
create_kanaal(kanaal, service)
self.stdout.write(
f"Registered kanaal '{kanaal}' with {service.api_root}"
)
except (
KanaalRequestException,
KanaalCreateException,
KanaalExistsException,
) as exception:
self.stderr.write(f"{str(exception)} . Skipping..")

0 comments on commit 253f750

Please sign in to comment.