According to the documentation for PyJWT, the class jwt.exceptions.InvalidTokenError is the base error when the decode method fails. However, the following code still breaks with different exceptions:
try:
jwt.decode(jwt_token, os.environ['SECRET'], algorithms="HS256")
except jwt.exceptions.InvalidTokenError:
pass
My thinking was that since InvalidTokenError is the base error, this except block should catch all the other possible PyJWT errors such as InvalidSignatureError, DecodeError etc. My question is if there is a base error for PyJwt I could use. I know using except Exception is always an option but that's bad form so I'd like to avoid it if possible. Thanks!
I got this micro example working. In general, don't pass an exception, at least print the error message.
Output:
Which error was raised? My best guess is that you have another problem. KeyError for Secret, is not an error related to jwt:
If your token is incorrect, you get this:
And if your signare is not correct, this: