def bungeni_custom_errors(f): """Decorator to intercept any error raised by function f and re-raise it as a BungeniCustomError. To be used to decorate any function involved in reading/validating/processing any bungeni_custom parameters. """ class BungeniCustomError(Exception): """A Localization Error. """ return error.exceptions_as(BungeniCustomError, True)(f)
def wrapped_callable(unwrapped): assert callable(unwrapped), unwrapped # Can re-use wrapped versions of *same* function -- on current config # (apr-2013), each wrapped function (on total of 45) is used more than # 5 times; with this each is created only once, as opposed to an # average of 5+ times. So, a little reduction in app runtime memory. if unwrapped in wrapped_callable.REUSE: wrapped_callable.REUSE_COUNT += 1 return wrapped_callable.REUSE[unwrapped] wrapped = error.exceptions_as(BungeniCustomRuntimeError, # a variety of sub-types of such an exception is *normal* and # expected and part of the internal implementation of zope.formlib # e.g. checkInvariants! So, may not re-raise them as another type... other_than=(zope.interface.Invalid,) )(unwrapped) # remember original unwrapped callable/details wrapped._unwrapped = unwrapped wrapped.__name__ = unwrapped.__name__ wrapped_callable.REUSE[unwrapped] = wrapped return wrapped