vimalloc/flask-jwt-extended

Unable to catch errors using flask @app.errorhandler

yuqiuwen opened this issue · 2 comments

I looked at the source code of this extended and found that set_error_handler_callbacks processed these errors using the hook @app.errorhandler In init app function. however, these errors cannot be caught by flask. I have to redefine the flask hook functions to handle jwt.execeptions, likeDecodeError, ExpiredSignatureError, etc.

I looked at the source code of this extended and found that set_error_handler_callbacks processed these errors using the hook @app.errorhandler In init app function. however, these errors cannot be caught by flask.

They absolutely can! If flask isn't catching these errors for you, you are probably using another extension that breaks the native flask error handler functionality (flask-restful and flask-restplus have historically been problematic in this regard). Unfortunately if another extension breaks behavior that is part of the core flask api there isn't really anything I can do about it in my extension. It will need to be fixed by whatever extension is causing the issues. However, you can see some potential workarounds in #86 that might help!

I looked at the source code of this extended and found that set_error_handler_callbacks processed these errors using the hook @app.errorhandler In init app function. however, these errors cannot be caught by flask.

They absolutely can! If flask isn't catching these errors for you, you are probably using another extension that breaks the native flask error handler functionality (flask-restful and flask-restplus have historically been problematic in this regard). Unfortunately if another extension breaks behavior that is part of the core flask api there isn't really anything I can do about it in my extension. It will need to be fixed by whatever extension is causing the issues. However, you can see some potential workarounds in #86 that might help!

Ok, I guess its not related to flask-jwt-extended. its also can not be handled by flask when i used pyjwt, which raise DecodeError, and return 422 http status code. Confusingly, i handle DecodeError manually, still useless

@app.errorhandler(ExpiredSignatureError)
def handle_expire_sign_error(error):
    app.logger.error(error)
    return make_response('sign expired', code=Unauthorized.code)

@app.errorhandler(DecodeError)
def handle_decode_error(error):
    app.logger.error(error)
    return make_response('decode error', code=Unauthorized.code)