Passed
Pull Request — master (#202)
by Vinicius
07:53
created

kytos.core.exceptions   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 95.74%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 128
ccs 45
cts 47
cp 0.9574
rs 10
c 0
b 0
f 0
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A KytosSwitchOfflineException.__str__() 0 5 1
A KytosNoTagAvailableError.__init__() 0 9 1
A KytosCoreException.__str__() 0 3 1
A KytosEventException.__init__() 0 10 1
A KytosNAppException.__init__() 0 8 1
A KytosNAppMissingInitArgument.__init__() 0 7 1
A KytosNoTagAvailableError.__str__() 0 4 1
A KytosSwitchOfflineException.__init__() 0 8 1
A KytosNAppException.__str__() 0 3 1
A KytosEventException.__str__() 0 6 2
A KytosDBInitException.__str__() 0 3 2
A KytosDBInitException.__init__() 0 5 1
1
"""Kytos Core-Defined Exceptions."""
2
3
4 2
class KytosCoreException(Exception):
5
    """Exception thrown when KytosCore is broken."""
6
7 2
    def __str__(self):
8
        """Return message of KytosCoreException."""
9 2
        return 'KytosCore exception: ' + super().__str__()
10
11
12 2
class KytosSwitchOfflineException(Exception):
13
    """Exception thrown when a switch is offline."""
14
15 2
    def __init__(self, switch):
16
        """Require a switch.
17
18
        Args:
19
            switch (:class:`~kytos.core.switch.Switch`): A switch offline.
20
        """
21 2
        super().__init__()
22 2
        self.switch = switch
23
24 2
    def __str__(self):
25
        """Return message of KytosSwitchOfflineException."""
26 2
        msg = 'The switch {} is not reachable. Please check the connection '
27 2
        msg += 'between the switch and the controller.'
28 2
        return msg.format(self.switch.dpid)
29
30
31 2
class KytosEventException(Exception):
32
    """Exception thrown when a KytosEvent have an illegal use."""
33
34 2
    def __init__(self, message="KytosEvent exception", event=None):
35
        """Assign parameters to instance variables.
36
37
        Args:
38
            message (string): message from KytosEventException.
39
            event (:class:`~kytos.core.events.KytosEvent`): Event malformed.
40
        """
41 2
        super().__init__()
42 2
        self.message = message
43 2
        self.event = event
44
45 2
    def __str__(self):
46
        """Return the full message from KytosEventException."""
47 2
        message = self.message
48 2
        if self.event:
49
            message += ". EventType: " + type(self.event)
50 2
        return message
51
52
53 2
class KytosWrongEventType(KytosEventException):
54
    """Exception related to EventType.
55
56
    When related to buffers, it means that the EventType is not allowed on
57
    that buffer.
58
    """
59
60
61 2
class KytosNoTagAvailableError(Exception):
62
    """Exception raised when a link has no vlan available."""
63
64 2
    def __init__(self, link):
65
        """Require a link.
66
67
        Args:
68
            link (:class:`~kytos.core.link.Link`): A link with no vlan
69
            available.
70
        """
71 2
        super().__init__()
72 2
        self.link = link
73
74 2
    def __str__(self):
75
        """Full message."""
76 2
        msg = f'Link {self.link.id} has no vlan available.'
77 2
        return msg
78
79
80 2
class KytosLinkCreationError(Exception):
81
    """Exception thrown when the link has an empty endpoint."""
82
83
84
# Exceptions related  to NApps
85
86
87 2
class KytosNAppException(Exception):
88
    """Exception raised on a KytosNApp."""
89
90 2
    def __init__(self, message="KytosNApp exception"):
91
        """Assign the parameters to instance variables.
92
93
        Args:
94
            message (string): message from KytosNAppException.
95
        """
96 2
        super().__init__()
97 2
        self.message = message
98
99 2
    def __str__(self):
100
        """Return the message from KytosNAppException."""
101 2
        return self.message
102
103
104 2
class KytosNAppMissingInitArgument(KytosNAppException):
105
    """Exception thrown when NApp have a missing init argument."""
106
107 2
    def __init__(self, message="KytosNAppMissingInitArgument"):
108
        """Assing parameters to instance variables.
109
110
        Args:
111
            message (str): Name of the missed argument.
112
        """
113
        super().__init__(message=message)
114
115
116 2
class KytosDBInitException(Exception):
117
    """Exception raised on Database initialization issues."""
118
119 2
    def __init__(self, message: str, _class=None) -> None:
120
        """KytosDBInitException."""
121 2
        super().__init__()
122 2
        self.message = message
123 2
        self._class = _class
124
125 2
    def __str__(self):
126 2
        _class = f" {self._class.__class__.__name__}" if self._class else ""
127
        return f"KytosDBInitException{_class}: {self.message}"
128