fix: make safeTraverse handle IndexErrors
This commit is contained in:
@@ -95,15 +95,15 @@ def translateLinks(link):
|
|||||||
def getUptime():
|
def getUptime():
|
||||||
return int(time.time()) - starttime
|
return int(time.time()) - starttime
|
||||||
|
|
||||||
def safeTraverse(obj: dict, path: list, default=None):
|
def safeTraverse(obj: dict, path: list, default=None, quiet: bool = False):
|
||||||
result = obj
|
result = obj
|
||||||
try:
|
try:
|
||||||
for x in path:
|
for x in path:
|
||||||
#print(f"traversing {result} with respect to {x}")
|
#print(f"traversing {result} with respect to {x}")
|
||||||
result = result[x]
|
result = result[x]
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError, IndexError):
|
||||||
result = default
|
result = default
|
||||||
print(f"error reading: {' -> '.join(path)} - returning: {default}")
|
if not quiet: print(f"error reading: {' -> '.join(path)} - returning: {default}")
|
||||||
finally:
|
finally:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user