Update app.py
Browse files
app.py
CHANGED
|
@@ -25,6 +25,32 @@ app = FastAPI()
|
|
| 25 |
# status_code=422,
|
| 26 |
# content={"detail": errors}
|
| 27 |
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Încărcăm modelul
|
| 30 |
try:
|
|
|
|
| 25 |
# status_code=422,
|
| 26 |
# content={"detail": errors}
|
| 27 |
# )
|
| 28 |
+
|
| 29 |
+
@app.exception_handler(ValidationError)
|
| 30 |
+
async def validation_exception_handler(request: Request, exc: ValidationError):
|
| 31 |
+
errors = []
|
| 32 |
+
for error in exc.errors():
|
| 33 |
+
# Extragem mesajul și eliminăm prefixul "Value error, "
|
| 34 |
+
message = error['msg']
|
| 35 |
+
if message.startswith("Value error, "):
|
| 36 |
+
message = message[12:] # Lungimea "Value error, " este 12
|
| 37 |
+
|
| 38 |
+
# Construim eroarea păstrând toate câmpurile originale,
|
| 39 |
+
# dar cu mesajul modificat
|
| 40 |
+
error_dict = {
|
| 41 |
+
"type": error.get('type'),
|
| 42 |
+
"loc": error.get('loc'),
|
| 43 |
+
"msg": message,
|
| 44 |
+
"input": error.get('input'),
|
| 45 |
+
"ctx": error.get('ctx'),
|
| 46 |
+
"url": error.get('url')
|
| 47 |
+
}
|
| 48 |
+
errors.append(error_dict)
|
| 49 |
+
|
| 50 |
+
return JSONResponse(
|
| 51 |
+
status_code=422,
|
| 52 |
+
content={"detail": errors}
|
| 53 |
+
)
|
| 54 |
|
| 55 |
# Încărcăm modelul
|
| 56 |
try:
|