Skip to content

Commit 759af30

Browse files
authored
fix: improve API validation error messages with request context (#1350)
feat: add more error message in api
1 parent d401ee7 commit 759af30

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/memos/api/exceptions.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@ class APIExceptionHandler:
1414
@staticmethod
1515
async def validation_error_handler(request: Request, exc: RequestValidationError):
1616
"""Handle request validation errors."""
17-
logger.error(f"Validation error: {exc.errors()}")
17+
errors = exc.errors()
18+
path = request.url.path
19+
method = request.method
20+
21+
readable_errors = []
22+
for err in errors:
23+
loc = " -> ".join(str(loc_i) for loc_i in err.get("loc", []))
24+
readable_errors.append(
25+
f"[{loc}] {err.get('msg', 'unknown error')} (type: {err.get('type', 'unknown')})"
26+
)
27+
28+
logger.error(
29+
f"Validation error on {method} {path}: {readable_errors}, raw errors: {errors}"
30+
)
1831
return JSONResponse(
1932
status_code=422,
2033
content={
2134
"code": 422,
22-
"message": "Parameter validation error",
23-
"detail": exc.errors(),
35+
"message": f"Parameter validation error on {method} {path}: {'; '.join(readable_errors)}",
36+
"detail": errors,
2437
"data": None,
2538
},
2639
)

0 commit comments

Comments
 (0)