diff --git a/backend/api.py b/backend/api.py index 9cf43ec..5803cd3 100644 --- a/backend/api.py +++ b/backend/api.py @@ -1,7 +1,9 @@ +from __future__ import annotations + import requests from backend import Route from models import Player, Alliance - +from .exceptions import AccessForbidden, ValidationError from typing import Dict __all__ = ("API",) @@ -27,9 +29,11 @@ def request( method, url, headers=headers, params=query_params, json=json, **kwargs ) if response.status_code == 403: - raise Exception("Access Forbidden") + raise AccessForbidden(response.status_code) # handle error here however you want to # error 403 & 401 are Access Forbidden + elif response.status_code == 422: + raise ValidationError(response.status_code, json) return response diff --git a/backend/exceptions.py b/backend/exceptions.py new file mode 100644 index 0000000..64ec24b --- /dev/null +++ b/backend/exceptions.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +__all__ = ("BackendException", "AccessForbidden", "ValidationError") + + +class BackendException(BaseException): + """Base exception for all exceptions in this module""" + + +class AccessForbidden(BackendException): + def __init__(self, status_code: int) -> None: + super().__init__(f"API returned {status_code}") + + +class ValidationError(BackendException): + def __init__(self, status_code: int, payload: dict) -> None: + self.payload = payload + super().__init__( + f"API returned {status_code}. This usually occurs to arguments not being specified or the API being unable to process this request. ", + payload, + )