diff --git a/jwt/api_jws.py b/jwt/api_jws.py index a61d22775..401bb91c8 100644 --- a/jwt/api_jws.py +++ b/jwt/api_jws.py @@ -113,14 +113,14 @@ def encode( key = alg_obj.prepare_key(key) signature = alg_obj.sign(signing_input, key) - except KeyError: + except KeyError as e: if not has_crypto and algorithm in requires_cryptography: raise NotImplementedError( "Algorithm '%s' could not be found. Do you have cryptography " "installed?" % algorithm - ) + ) from e else: - raise NotImplementedError("Algorithm not supported") + raise NotImplementedError("Algorithm not supported") from e segments.append(base64url_encode(signature)) @@ -236,8 +236,8 @@ def _verify_signature( if not alg_obj.verify(signing_input, key, signature): raise InvalidSignatureError("Signature verification failed") - except KeyError: - raise InvalidAlgorithmError("Algorithm not supported") + except KeyError as e: + raise InvalidAlgorithmError("Algorithm not supported") from e def _validate_headers(self, headers): if "kid" in headers: