WIP: Custom exceptions handling and new models

This commit is contained in:
2023-02-16 14:11:29 +01:00
parent dddb5dbc12
commit 873e506c7d
7 changed files with 147 additions and 37 deletions

46
classes/exceptions.py Normal file
View File

@@ -0,0 +1,46 @@
from typing import Literal
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
class AlbumNotFoundError(Exception):
def __init__(self, id: str):
self.id = id
self.openapi = {
"description": "Album does not exist",
"content": {
"application/json": {
"example": {
"detail": "Could not find an album with id '{id}'."
}
}
}
}
class AlbumAlreadyExistsError(Exception):
def __init__(self, name: str):
self.name = name
self.openapi = {
"description": "Album already exists",
"content": {
"application/json": {
"example": {
"detail": "Album with name '{name}' already exists."
}
}
}
}
class AlbumIncorrectError(Exception):
def __init__(self, place: Literal["name", "title"], error: str) -> None:
self.place = place
self.error = error
self.openapi = {
"description": "Album name/title invalid",
"content": {
"application/json": {
"example": {
"detail": "Album {name/title} invalid: {error}"
}
}
}
}