From 1a5b2d166fc95e5f3f07fdfec075acdf4d0eda92 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Fri, 19 Jun 2020 23:39:00 +0300 Subject: Fix exception causes all over the codebase The mistake is this: In some parts of the code, an exception is being caught and replaced with a more user-friendly error. In these cases the syntax `raise new_error from old_error` needs to be used. Python's exception chaining means it shows not only the traceback of the current exception, but that of the original exception (and possibly more.) This is regardless of `raise from`. The usage of `raise from` tells Python to put a more accurate message between the tracebacks. Instead of this: During handling of the above exception, another exception occurred: You'll get this: The above exception was the direct cause of the following exception: The first is inaccurate, because it signifies a bug in the exception-handling code itself, which is a separate situation than wrapping an exception. --- rsa/pkcs1_v2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'rsa/pkcs1_v2.py') diff --git a/rsa/pkcs1_v2.py b/rsa/pkcs1_v2.py index f780aff..ed616f5 100644 --- a/rsa/pkcs1_v2.py +++ b/rsa/pkcs1_v2.py @@ -49,12 +49,12 @@ def mgf1(seed: bytes, length: int, hasher: str = 'SHA-1') -> bytes: try: hash_length = pkcs1.HASH_METHODS[hasher]().digest_size - except KeyError: + except KeyError as ex: raise ValueError( 'Invalid `hasher` specified. Please select one of: {hash_list}'.format( hash_list=', '.join(sorted(pkcs1.HASH_METHODS.keys())) ) - ) + ) from ex # If l > 2^32(hLen), output "mask too long" and stop. if length > (2**32 * hash_length): -- cgit v1.2.1