ospd.errors   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 19
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A OspdCommandError.__init__() 0 7 1
A RequiredArgument.__init__() 0 4 1
A RequiredArgument.__str__() 0 3 1
A OspdCommandError.as_xml() 0 3 1
1
# Copyright (C) 2014-2021 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: AGPL-3.0-or-later
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Affero General Public License as
7
# published by the Free Software Foundation, either version 3 of the
8
# License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
""" OSP class for handling errors.
19
"""
20
21
from ospd.xml import simple_response_str
22
23
24
class OspdError(Exception):
25
    """Base error class for all Ospd related errors"""
26
27
28
class RequiredArgument(OspdError):
29
    """Raised if a required argument/parameter is missing
30
31
    Derives from :py:class:`OspdError`
32
    """
33
34
    def __init__(self, function: str, argument: str) -> None:
35
        # pylint: disable=super-init-not-called
36
        self.function = function
37
        self.argument = argument
38
39
    def __str__(self) -> str:
40
        return "{}: Argument {} is required".format(
41
            self.function, self.argument
42
        )
43
44
45
class OspdCommandError(OspdError):
46
47
    """This is an exception that will result in an error message to the
48
    client"""
49
50
    def __init__(
51
        self, message: str, command: str = 'osp', status: int = 400
52
    ) -> None:
53
        super().__init__(message)
54
        self.message = message
55
        self.command = command
56
        self.status = status
57
58
    def as_xml(self) -> str:
59
        """Return the error in xml format."""
60
        return simple_response_str(self.command, self.status, self.message)
61