Passed
Pull Request — master (#192)
by Jaspar
02:22
created

gvm.errors.GvmResponseError.__str__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 - 2019 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
"""
19
Module for GVM errors
20
"""
21
22
from typing import Optional
23
24
25
class GvmError(Exception):
26
    """An exception for gvm errors
27
28
    Base class for all exceptions originating in python-gvm.
29
    """
30
31
32
class GvmClientError(GvmError):
33
    """An exception for gvm client errors
34
35
    Base class for all exceptions originating in python-gvm.
36
    """
37
38
39
class GvmServerError(GvmError):
40
    """An exception for gvm server errors
41
42
    Derives from :py:class:`GvmError`
43
44
    Arguments:
45
        status:  The HTTP response status
46
        message: Error message to be displayed. Takes precedence over argument
47
            and function
48
    """
49
50
    def __init__(self, status: str = None, message: str = None):
51
        # pylint: disable=super-init-not-called
52
        self.status = status
53
        self.message = message
54
55
    def __str__(self):
56
        return (
57
            self.__class__.__name__ + ': ' + self.status + ' - ' + self.message
58
        )
59
60
61
class GvmResponseError(GvmClientError):
62
    """An exception for gvm server errors
63
64
    Derives from :py:class:`GvmClientError`
65
66
    Arguments:
67
        status:  The HTTP response status
68
        message: Error message to be displayed. Takes precedence over argument
69
            and function
70
    """
71
72
    def __init__(self, status: str = None, message: str = None):
73
        # pylint: disable=super-init-not-called
74
        self.status = status
75
        self.message = message
76
77
    def __str__(self):
78
        return (
79
            self.__class__.__name__ + ': ' + self.status + ' - ' + self.message
80
        )
81
82
83 View Code Duplication
class InvalidArgument(GvmError):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
84
    """Raised if an invalid argument/parameter is passed
85
86
    Derives from :py:class:`GvmError`
87
88
    Arguments:
89
        message: Error message to be displayed. Takes precedence over argument
90
            and function
91
        argument: Optional name of the invalid argument
92
        function: Optional name of the called function
93
    """
94
95
    def __init__(
96
        self,
97
        message: Optional[str] = None,
98
        *,
99
        argument: Optional[str] = None,
100
        function: Optional[str] = None
101
    ):
102
        # pylint: disable=super-init-not-called
103
        self.message = message
104
        self.argument = argument
105
        self.function = function
106
107
    def __str__(self):
108
        if self.message:
109
            return self.message
110
111
        if not self.function:
112
            return "Invalid argument {}".format(self.argument)
113
114
        if not self.argument:
115
            return "Invalid argument for {}".format(self.function)
116
117
        return "Invalid argument {} for {}".format(self.argument, self.function)
118
119
120
class InvalidArgumentType(GvmError):
121
    """Raised if a passed argument has an invalid type
122
123
    Derives from :py:class:`GvmError`
124
125
    Arguments:
126
        argument: Name of the invalid argument
127
        arg_type: The correct argument type
128
        function: Optional name of the called function
129
    """
130
131
    def __init__(
132
        self,
133
        argument: str = None,
134
        arg_type: str = None,
135
        *,
136
        function: Optional[str] = None
137
    ):
138
        # pylint: disable=super-init-not-called
139
        self.argument = argument
140
        self.function = function
141
        self.arg_type = arg_type
142
143
    def __str__(self):
144
        if self.function:
145
            return "In {} the argument {} must be of type {}.".format(
146
                self.function, self.argument, self.arg_type
147
            )
148
        return "The argument {} must be of type {}.".format(
149
            self.argument, self.arg_type
150
        )
151
152
153 View Code Duplication
class RequiredArgument(GvmError):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
154
    """Raised if a required argument/parameter is missing
155
156
    Derives from :py:class:`GvmError`
157
158
    Arguments:
159
        message: Error message to be displayed. Takes precedence over argument
160
            and function.
161
        argument: Optional name of the required argument.
162
        function: Optional name of the called function.
163
    """
164
165
    def __init__(
166
        self,
167
        message: Optional[str] = None,
168
        *,
169
        argument: Optional[str] = None,
170
        function: Optional[str] = None
171
    ):
172
        # pylint: disable=super-init-not-called
173
        self.message = message
174
        self.argument = argument
175
        self.function = function
176
177
    def __str__(self):
178
        if self.message:
179
            return self.message
180
181
        if not self.function:
182
            return "Required argument {}".format(self.argument)
183
184
        if not self.argument:
185
            return "Required argument missing for {}".format(self.function)
186
187
        return "{} requires a {} argument".format(self.function, self.argument)
188