jpadilla / pyjwt

JSON Web Token implementation in Python
https://pyjwt.readthedocs.io
MIT License
4.99k stars 676 forks source link

Unable to catch errors using flask @app.errorhandler #920

Closed yuqiuwen closed 7 months ago

yuqiuwen commented 9 months ago

Unable to catch it when raise errors, like DecodeError, InvalidTokenError, etc.

Expected Result

these errors are caught by flask error handler

Actual Result

return 422 http status code, and these errors are not caught by any hook function. { "msg": "Signature verification failed" }

Reproduction Steps

import jwt
from jwt.exceptions import InvalidTokenError, DecodeError, InvalidSignatureError, ExpiredSignatureError

@app.errorhandler(InvalidSignatureError)
def handle_invalid_sign_error(error):
    app.logger.error(error)
    return make_response('invalid sign', code=Unauthorized.code)

@app.errorhandler(InvalidTokenError)
def handle_invalid_token_error(error):
    app.logger.error(error)
    return make_response('invalid token', code=Unauthorized.code)

@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)

System Information

$ python -m jwt.help
{
  "cryptography": {
    "version": ""
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.17"
  },
  "platform": {
    "release": "22.5.0",
    "system": "Darwin"
  },
  "pyjwt": {
    "version": "2.8.0"
  }
}

This command is only available on PyJWT v1.6.3 and greater. Otherwise, please provide some basic information about your system.

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days