# @/main.py from fastapi import FastAPI, Request, status from fastapi.responses import JSONResponse from fastapi.templating import Jinja2Templates from exceptions import UserNotFoundError, UserAlreadyExistsError, UnauthorizedError, ForbiddenError, ValidationError #Errors import models #Database from routers import api_router #API app = FastAPI(title="Tandur API") app.include_router(api_router) templates = Jinja2Templates(directory="templates") ######### ERROR handling ######### # 1. Globale Regel für 404 @app.exception_handler(UserNotFoundError) def user_not_found_handler(request: Request, exc: UserNotFoundError): return JSONResponse( status_code=status.HTTP_404_NOT_FOUND, content={"detail": str(exc)}, ) # 2. Globale Regel für 409 @app.exception_handler(UserAlreadyExistsError) def user_exists_handler(request: Request, exc: UserAlreadyExistsError): return JSONResponse( status_code=status.HTTP_409_CONFLICT, content={"detail": str(exc)}, ) @app.exception_handler(UnauthorizedError) def unauthorized_handler(request: Request, exc: UnauthorizedError): return JSONResponse( status_code=status.HTTP_401_UNAUTHORIZED, content={"detail": str(exc)}, headers={"WWW-Authenticate": "Bearer"}, ) @app.exception_handler(ForbiddenError) def permissions_handler(request: Request, exc: ForbiddenError): return JSONResponse( status_code=status.HTTP_403_FORBIDDEN, content={"detail": str(exc)}, ) @app.exception_handler(ValidationError) def permissions_handler(request: Request, exc: ValidationError): return JSONResponse( status_code=status.HTTP_400_BAD_REQUEST, content={"detail": str(exc)}, ) @app.get("/") async def home(request: Request): return templates.TemplateResponse(request=request, name="index.html")