Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-11: Add exceptions to backend (merge with feature/backend) #14

Merged
merged 5 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)