-
Hello, I'm wondering if there is a way to change error handler for ValidationError for just single endpoint. I'm using Pydantic's model validator to check if user exists in database to keep the error messages consistent and my endpoint should return 401 status code instead of standard 422. |
Beta Was this translation helpful? Give feedback.
Answered by
vitalik
Oct 24, 2024
Replies: 1 comment 1 reply
-
Hi @flisakl basically there are two options:
class MyException(Exception):
pass
@api.exception_handler(MyException)
def custom_401_handler(request, exc):
return api.create_response(
request,
{"message": "Invalid credentials or something"},
status=401,
)
@api.post("/some")
def some_view(request):
if something: # Example: user does not exist
raise MyException("User not found")
return {"detail": "User exists"}
@api.post("/some", response={200: SomeSchema, 401: OtherSchema}
def some(request):
if some:
return 401, {"some": "detail"}
return {"normal": "response"} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
flisakl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @flisakl
basically there are two options: