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