|  | 
| 1 |  | -from json import loads | 
| 2 | 1 | from fastapi import FastAPI, Request | 
| 3 |  | -from fastapi.exceptions import RequestValidationError, ValidationError | 
|  | 2 | +from fastapi.exceptions import RequestValidationError | 
|  | 3 | +from fastapi.exception_handlers import ( | 
|  | 4 | +    http_exception_handler, | 
|  | 5 | +    request_validation_exception_handler, | 
|  | 6 | +) | 
| 4 | 7 | from starlette.middleware.cors import CORSMiddleware | 
| 5 |  | -from starlette.responses import JSONResponse | 
|  | 8 | +from starlette.exceptions import HTTPException | 
|  | 9 | +from starlette.responses import Response | 
| 6 | 10 | from apscheduler.schedulers.background import BackgroundScheduler | 
| 7 | 11 | from app.controllers import db_updater | 
| 8 | 12 | from app.router import api_router | 
| 9 | 13 | from app.services import create_indexes | 
| 10 |  | -from app.utils import json_encoder | 
| 11 | 14 | 
 | 
| 12 | 15 | DESCRIPTION = ''' | 
| 13 | 16 | A backend for dependency graph building, atribution of vulnerabilities and reasoning | 
| @@ -39,15 +42,14 @@ async def startup_event() -> None: | 
| 39 | 42 |     scheduler.start() | 
| 40 | 43 | 
 | 
| 41 | 44 | 
 | 
|  | 45 | +@app.exception_handler(HTTPException) | 
|  | 46 | +async def custom_http_exception_handler(request: Request, exc: HTTPException) -> Response: | 
|  | 47 | +    return await http_exception_handler(request, exc) | 
|  | 48 | + | 
|  | 49 | + | 
| 42 | 50 | @app.exception_handler(RequestValidationError) | 
| 43 |  | -@app.exception_handler(ValidationError) | 
| 44 |  | -async def validation_exception_handler(_: Request, exc: ValidationError | RequestValidationError) -> JSONResponse: | 
| 45 |  | -    exc_json = loads(exc.json()) | 
| 46 |  | -    response: dict[str, list[str]] = {'message': []} | 
| 47 |  | -    for error in exc_json: | 
| 48 |  | -        response['message'].append(error['loc'][-1] + f": {error['msg']}") | 
| 49 |  | - | 
| 50 |  | -    return JSONResponse(content=json_encoder(response), status_code=422) | 
|  | 51 | +async def validation_exception_handler(request: Request, exc: RequestValidationError) -> Response: | 
|  | 52 | +    return await request_validation_exception_handler(request, exc) | 
| 51 | 53 | 
 | 
| 52 | 54 | app.add_middleware( | 
| 53 | 55 |     CORSMiddleware, | 
|  | 
0 commit comments