Skip to content

Commit

Permalink
GH-11: Add exceptions to backend (merge with feature/backend) (#14)
Browse files Browse the repository at this point in the history
* implement exceptions

Signed-off-by: ItsNeil17 <[email protected]>

* use implemented exceptions rather than Exception

Signed-off-by: ItsNeil17 <[email protected]>

* fix linting

Signed-off-by: ItsNeil17 <[email protected]>

* fix linting

Signed-off-by: ItsNeil17 <[email protected]>

---------

Signed-off-by: ItsNeil17 <[email protected]>
Signed-off-by: Neil <[email protected]>
  • Loading branch information
ItsNeil17 authored Jun 9, 2024
1 parent 089da9c commit be073fe
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions backend/api.py
Original file line number Diff line number Diff line change
@@ -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",)
Expand All @@ -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

Expand Down
21 changes: 21 additions & 0 deletions backend/exceptions.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit be073fe

Please sign in to comment.