def mute_errors(self, fn, *args, **kwargs):
"""Run given function and catch any of the errors it logged.
The self._errors key will not be changed by using this function.
Works by patching the BaseValidator.log_error function.
BaseValidator.log_error should not be overridden in children.
"""
caught_errors = set()
def patched_log_error(self, field, msg):
tb = traceback.extract_stack()[:-1] if self.debug else None
caught_errors.add(ValidatorLogError(msg, self._path + (field,), tb))
old_log_error = BaseValidator.log_error
try:
BaseValidator.log_error = patched_log_error
val = fn(*args, **kwargs)
finally:
BaseValidator.log_error = old_log_error
return val, caught_errors
评论列表
文章目录