コード例 #1
0
ファイル: decorators.py プロジェクト: suncle1993/hutils
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except exceptions as ex:
         if log:
             log_error(__name__, ex)
         return returns
コード例 #2
0
def catches(*exceptions, raises: Union[BaseException, Callable[[Exception], BaseException]], logger=None):
    """ 封装转换错误类。transfer exceptions to a different type.

    Examples::

        with self.assertRaises(IOError), catches(ValueError, TypeError, raises=IOError()):
            raise ValueError('should wrap this error')

        @catches(raises=get_validation_error, log=True)
        def raise_io_error():
            raise ValueError('should wrap this error')
    """
    exceptions = exceptions or (Exception,)
    try:
        yield
    except exceptions as ex:
        if callable(raises):
            raises = raises(ex)
        if logger:
            log_error(logger, raises)
        raise raises from ex
コード例 #3
0
ファイル: decorators.py プロジェクト: zaihui/hutils
 def __exit__(self, exc_type, exc_val, exc_tb):
     if any([exc for exc in self.exceptions if isinstance(exc_val, exc)]):
         if self.logger:
             log_error(self.logger, exc_val)
         return True
     return False
コード例 #4
0
ファイル: decorators.py プロジェクト: suncle1993/hutils
def ignore_error(*exceptions, logger=None):
    exceptions = exceptions or (Exception, )
    try:
        yield
    except exceptions as ex:
        log_error(logger or __name__, ex)