1 | # -*- coding: utf-8 -*- |
||
2 | # Copyright (C) 2018-2021 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 | def __init__(self, message: str, *args): |
||
32 | super().__init__(message, *args) |
||
33 | self.message = message |
||
34 | |||
35 | def __repr__(self): |
||
36 | return '<{} message="{}">'.format(self.__class__.__name__, self.message) |
||
37 | |||
38 | def __str__(self): |
||
39 | return self.message |
||
40 | |||
41 | |||
42 | class GvmClientError(GvmError): |
||
43 | """An exception for gvm client errors |
||
44 | |||
45 | Base class for all exceptions originating in python-gvm. |
||
46 | """ |
||
47 | |||
48 | |||
49 | class GvmServerError(GvmError): |
||
50 | """An exception for gvm server errors |
||
51 | |||
52 | Derives from :py:class:`GvmError` |
||
53 | |||
54 | Arguments: |
||
55 | status: The HTTP response status |
||
56 | message: Error message to be displayed. Takes precedence over argument |
||
57 | and function |
||
58 | """ |
||
59 | |||
60 | def __init__(self, status: str = None, message: str = None): |
||
61 | super().__init__(message, status) |
||
62 | self.status = status |
||
63 | |||
64 | def __str__(self): |
||
65 | return 'Server Error {}. {}'.format(self.status, self.message) |
||
66 | |||
67 | def __repr__(self): |
||
68 | return '<{} status="{}" message="{}">'.format( |
||
69 | self.__class__.__name__, self.status, self.message |
||
70 | ) |
||
71 | |||
72 | |||
73 | class GvmResponseError(GvmClientError): |
||
74 | """An exception for gvm server errors |
||
75 | |||
76 | Derives from :py:class:`GvmClientError` |
||
77 | |||
78 | Arguments: |
||
79 | status: The HTTP response status |
||
80 | message: Error message to be displayed. Takes precedence over argument |
||
81 | and function |
||
82 | """ |
||
83 | |||
84 | def __init__(self, status: str = None, message: str = None): |
||
85 | super().__init__(message, status) |
||
86 | self.status = status |
||
87 | |||
88 | def __str__(self): |
||
89 | return 'Response Error {}. {}'.format(self.status, self.message) |
||
90 | |||
91 | def __repr__(self): |
||
92 | return '<{} status="{}" message="{}">'.format( |
||
93 | self.__class__.__name__, self.status, self.message |
||
94 | ) |
||
95 | |||
96 | |||
97 | View Code Duplication | class InvalidArgument(GvmError): |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
98 | """Raised if an invalid argument/parameter is passed |
||
99 | |||
100 | Derives from :py:class:`GvmError` |
||
101 | |||
102 | Arguments: |
||
103 | message: Error message to be displayed. Takes precedence over argument |
||
104 | and function |
||
105 | argument: Optional name of the invalid argument |
||
106 | function: Optional name of the called function |
||
107 | """ |
||
108 | |||
109 | def __init__( |
||
110 | self, |
||
111 | message: Optional[str] = None, |
||
112 | *, |
||
113 | argument: Optional[str] = None, |
||
114 | function: Optional[str] = None, |
||
115 | ): |
||
116 | super().__init__(message, argument, function) |
||
117 | self.argument = argument |
||
118 | self.function = function |
||
119 | |||
120 | def __str__(self): |
||
121 | if self.message: |
||
122 | return self.message |
||
123 | |||
124 | if not self.function: |
||
125 | return "Invalid argument {}".format(self.argument) |
||
126 | |||
127 | if not self.argument: |
||
128 | return "Invalid argument for {}".format(self.function) |
||
129 | |||
130 | return "Invalid argument {} for {}".format(self.argument, self.function) |
||
131 | |||
132 | |||
133 | class InvalidArgumentType(GvmError): |
||
134 | """Raised if a passed argument has an invalid type |
||
135 | |||
136 | Derives from :py:class:`GvmError` |
||
137 | |||
138 | Arguments: |
||
139 | argument: Name of the invalid argument |
||
140 | arg_type: The correct argument type |
||
141 | function: Optional name of the called function |
||
142 | """ |
||
143 | |||
144 | def __init__( |
||
145 | self, |
||
146 | argument: str = None, |
||
147 | arg_type: str = None, |
||
148 | *, |
||
149 | function: Optional[str] = None, |
||
150 | ): |
||
151 | # pylint: disable=super-init-not-called |
||
152 | self.argument = argument |
||
153 | self.function = function |
||
154 | self.arg_type = arg_type |
||
155 | |||
156 | def __str__(self): |
||
157 | if self.function: |
||
158 | return "In {} the argument {} must be of type {}.".format( |
||
159 | self.function, self.argument, self.arg_type |
||
160 | ) |
||
161 | return "The argument {} must be of type {}.".format( |
||
162 | self.argument, self.arg_type |
||
163 | ) |
||
164 | |||
165 | |||
166 | View Code Duplication | class RequiredArgument(GvmError): |
|
0 ignored issues
–
show
|
|||
167 | """Raised if a required argument/parameter is missing |
||
168 | |||
169 | Derives from :py:class:`GvmError` |
||
170 | |||
171 | Arguments: |
||
172 | message: Error message to be displayed. Takes precedence over argument |
||
173 | and function. |
||
174 | argument: Optional name of the required argument. |
||
175 | function: Optional name of the called function. |
||
176 | """ |
||
177 | |||
178 | def __init__( |
||
179 | self, |
||
180 | message: Optional[str] = None, |
||
181 | *, |
||
182 | argument: Optional[str] = None, |
||
183 | function: Optional[str] = None, |
||
184 | ): |
||
185 | super().__init__(message, argument, function) |
||
186 | self.argument = argument |
||
187 | self.function = function |
||
188 | |||
189 | def __str__(self): |
||
190 | if self.message: |
||
191 | return self.message |
||
192 | |||
193 | if not self.function: |
||
194 | return "Required argument {}".format(self.argument) |
||
195 | |||
196 | if not self.argument: |
||
197 | return "Required argument missing for {}".format(self.function) |
||
198 | |||
199 | return "{} requires a {} argument".format(self.function, self.argument) |
||
200 |