Completed
Push — master ( 8b993b...d268b8 )
by Olivier
01:01
created

trycatch()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
1
2
import inspect
3
import logging
4
5
6
logger = logging.getLogger(__name__)
7
8
9
def trycatchslot(func):
10
    """
11
    wrap a at slot.
12
    log and call a method called show_error in 
13
    case of error
14
    """
15
    def wrapper(self, *args):
16
        # filter out excess args as qt signals do
17
        sig = inspect.signature(func)
18
        args = args[:(len(sig.parameters)-1)]
19
        result = None
20
        try:
21
            result = func(self, *args)
22
        except Exception as ex:
23
            logger.exception(ex)
24
            if hasattr(self, "show_error"):
25
                self.show_error(ex)
26
            elif hasattr(self, "error"):
27
                self.error.emit(ex)
28
            else:
29
                logger.warning("Error class % has no member show_error or error", self)
30
        return result
31
    return wrapper
32
33
34