WIP: Custom exceptions handling and new models
This commit is contained in:
46
classes/exceptions.py
Normal file
46
classes/exceptions.py
Normal 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}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user