Add specific exceptions for missing id & unfetched links

This commit is contained in:
Peter Vacho 2024-12-25 22:26:32 +01:00
parent cb5d6a63ee
commit 9002804b8d
Signed by: school
GPG key ID: 8CFC3837052871B4
4 changed files with 35 additions and 5 deletions

View file

@ -11,6 +11,7 @@ from src.api.auth.dependencies import LoggedInDep
from src.db.models.category import Category
from src.db.models.event import Event
from src.db.models.user import User
from src.utils.db import MissingIdError
from src.utils.logging import get_logger
from .auth import CurrentUserDep
@ -49,7 +50,7 @@ class CategoryData(_BaseCategoryData):
def from_category(cls, category: Category) -> "CategoryData":
"""Construct CategoryData from an instance of Category."""
if category.id is None:
raise ValueError("Got a category without id")
raise MissingIdError(category)
return cls(
name=category.name,

View file

@ -7,6 +7,7 @@ from pydantic import BaseModel
from src.api.auth.jwt import invalidate_access_tokens
from src.db.models.token import Token
from src.utils.db import MissingIdError, UnfetchedLinkError
from src.utils.logging import get_logger
from .auth import AnyTokenDep, CurrentUserDep
@ -36,15 +37,15 @@ class UserSession(BaseModel):
# This can only happen if the attendee wasn't yet committed to the db,
# within the context of this function, that should never happen.
if token.id is None:
raise ValueError("Got token without id")
raise MissingIdError(token)
if token.parent_token:
# This function expects the parent token links to be fetched already
if isinstance(token.parent_token, Link):
raise TypeError("Parent token must be fetched before calling this function")
raise UnfetchedLinkError(token.parent_token)
if token.parent_token.id is None:
raise ValueError("Got parent token without id")
raise MissingIdError(token.parent_token)
return cls(
id=str(token.id),

View file

@ -10,6 +10,7 @@ from src.api.auth.dependencies import LoggedInDep
from src.api.auth.passwords import check_hashed_password, create_password_hash
from src.db.models.token import Token
from src.db.models.user import User
from src.utils.db import MissingIdError
from src.utils.logging import get_logger
from .auth import CurrentUserDep
@ -50,7 +51,7 @@ class UserData(BaseModel):
def from_user(cls, user: User) -> "UserData":
"""Construct UserData from a User instance."""
if user.id is None:
raise ValueError("Got a user without id")
raise MissingIdError(user)
return cls(
user_id=user.id,

27
src/utils/db.py Normal file
View file

@ -0,0 +1,27 @@
from typing import final
from beanie import Document, Link
@final
class MissingIdError(ValueError):
"""Raised when an item is missing an id.
This usually happens with objects that were not yet committed to the db.
"""
def __init__(self, item: Document):
self.item = item
super().__init__(f"Item {item} is missing an id")
@final
class UnfetchedLinkError[T](ValueError):
"""Raised when a link object is found, where a document was expected.
This usually happens when an object is fetched from the db without link fetching enabled.
"""
def __init__(self, link: Link[T]):
self.link = link
super().__init__(f"Link {link} was not fetched")