Skip to content

Commit a2066d5

Browse files
authored
Merge pull request #199 from bjoernricks/unify-error-classes
Unify error classes
2 parents 5358131 + c9be2a8 commit a2066d5

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
default and also python-gvm in editable mode. This means after running
1818
`poetry install` gvm will directly be importable in the virtual python
1919
environment. [#197](https://github.com/greenbone/python-gvm/pull/197)
20+
* Update error classes to always have meaningful `__str__` and `__repr__`
21+
method. This allows for easier error printing
22+
[#199](https://github.com/greenbone/python-gvm/pull/199)
2023

2124
[unreleased]: https://github.com/greenbone/python-gvm/compare/v1.3.0...master
2225

gvm/errors.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class GvmError(Exception):
2828
Base class for all exceptions originating in python-gvm.
2929
"""
3030

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+
3141

3242
class GvmClientError(GvmError):
3343
"""An exception for gvm client errors
@@ -48,13 +58,15 @@ class GvmServerError(GvmError):
4858
"""
4959

5060
def __init__(self, status: str = None, message: str = None):
51-
# pylint: disable=super-init-not-called
61+
super().__init__(message, status)
5262
self.status = status
53-
self.message = message
5463

5564
def __str__(self):
56-
return (
57-
self.__class__.__name__ + ': ' + self.status + ' - ' + self.message
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
5870
)
5971

6072

@@ -70,13 +82,15 @@ class GvmResponseError(GvmClientError):
7082
"""
7183

7284
def __init__(self, status: str = None, message: str = None):
73-
# pylint: disable=super-init-not-called
85+
super().__init__(message, status)
7486
self.status = status
75-
self.message = message
7687

7788
def __str__(self):
78-
return (
79-
self.__class__.__name__ + ': ' + self.status + ' - ' + self.message
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
8094
)
8195

8296

@@ -99,8 +113,7 @@ def __init__(
99113
argument: Optional[str] = None,
100114
function: Optional[str] = None
101115
):
102-
# pylint: disable=super-init-not-called
103-
self.message = message
116+
super().__init__(message, argument, function)
104117
self.argument = argument
105118
self.function = function
106119

@@ -169,8 +182,7 @@ def __init__(
169182
argument: Optional[str] = None,
170183
function: Optional[str] = None
171184
):
172-
# pylint: disable=super-init-not-called
173-
self.message = message
185+
super().__init__(message, argument, function)
174186
self.argument = argument
175187
self.function = function
176188

tests/test_errors.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ def test_is_gvm_error(self):
168168

169169
class GvmServerErrorTestCase(unittest.TestCase):
170170
def test_raise_with_message_and_status(self):
171-
with self.assertRaisesRegex(
172-
GvmServerError, '^GvmServerError: foo - bar$'
173-
):
171+
with self.assertRaisesRegex(GvmServerError, '^Server Error foo. bar$'):
174172
raise GvmServerError('foo', 'bar')
175173

176174
def test_is_gvm_error(self):
@@ -181,7 +179,7 @@ def test_is_gvm_error(self):
181179
class GvmResponseErrorTestCase(unittest.TestCase):
182180
def test_raise_with_message_and_status(self):
183181
with self.assertRaisesRegex(
184-
GvmResponseError, '^GvmResponseError: foo - bar$'
182+
GvmResponseError, '^Response Error foo. bar$'
185183
):
186184
raise GvmResponseError('foo', 'bar')
187185

0 commit comments

Comments
 (0)