Skip to content

Commit 96434d0

Browse files
committed
create common exceptions handler for validation error
1 parent b169491 commit 96434d0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from functools import wraps
2+
3+
from fastapi import HTTPException, status
4+
from pydantic import ValidationError
5+
6+
7+
def handle_validation_error(func):
8+
@wraps(func)
9+
def wrapper(*args, **kwargs):
10+
try:
11+
return func(*args, **kwargs)
12+
except ValidationError as ex:
13+
raise HTTPException(
14+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
15+
detail=ex.errors(),
16+
)
17+
18+
return wrapper

0 commit comments

Comments
 (0)