Having a verification helper function (which would be called by various testcases with different boolean values passed for raise_on_error):
def verify_multiply(a: int, b: int, c: int, raise_on_error=True):
if a * b == c:
return True
else:
error_msg = f"{a} * {b} != {c}"
if raise_on_error:
raise ValueError(error_msg)
else:
log.error(error_msg)
return False
I wanted to replace the error handler with a wrapper like:
def error_handler(error_msg, raise_on_error=True):
if raise_on_error:
raise ValueError(error_msg)
else:
log.error(error_msg)
return False
So that the original verifier could be written into:
def verify_multiply(a: int, b: int, c: int, raise_on_error=True):
if a * b == c:
return True
else:
error_msg = f"{a} * {b} != {c}"
error_hanlder(error_msg, raise_on_error)
But on second thought, when raise_on_error is False, error_handler will return False to verify_multiply, not from verify_multiply to its caller (which is my intent), right?
Is there any way to wrap the error handling code into a separate function to be re-used, and let False be returned to verify_multiply's caller when raise_on_error is not True?
I am asking this question because the error handling snippet was needed by a handful verifiers, and it feels awkward to repeat the same code over and over. But the return point is my concern.