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