Skip to content

Is there a way to change error handler for single endpoint? #1325

Answered by vitalik
flisakl asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @flisakl

basically there are two options:

  1. define custom exception handler
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"}
  1. do it in operations and define in docs:
@api.post("/some", response={200: SomeSchema, 401: OtherSchema}
def some(request):
    if some:
        return 401, {"some": "detail"}
   return {"normal": "…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@flisakl
Comment options

Answer selected by flisakl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants