Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit c04ef0a

Browse files
committed
proper use of exception chaining
1 parent 58ff311 commit c04ef0a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

multihash.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def __new__(cls, func, digest):
268268
elif func in _func_from_name:
269269
f = _func_from_name[func] # function name
270270
else:
271-
raise ValueError("invalid hash function code", func)
271+
raise ValueError("invalid hash function code", func) from ve
272272
return super(cls, Multihash).__new__(cls, f, bytes(digest))
273273

274274
@classmethod
@@ -291,8 +291,9 @@ def from_hash(self, hash):
291291
"""
292292
try:
293293
func = FuncHash.func_from_hash(hash)
294-
except KeyError:
295-
raise ValueError("no matching multihash function", hash.name)
294+
except KeyError as ke:
295+
raise ValueError(
296+
"no matching multihash function", hash.name) from ke
296297
digest = hash.digest()
297298
return Multihash(func, digest)
298299

@@ -392,8 +393,8 @@ def decode(mhash, encoding=None):
392393
func = mhash[0]
393394
length = mhash[1]
394395
digest = mhash[2:]
395-
except IndexError:
396-
raise ValueError("multihash is too short")
396+
except IndexError as ie:
397+
raise ValueError("multihash is too short") from ie
397398
if length != len(digest):
398399
raise ValueError(
399400
"multihash length field does not match digest field length")

0 commit comments

Comments
 (0)