Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend InvalidArgument and RequiredArgument error classes #134

Merged
merged 4 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
methods in Gmpv8 [PR 133](https://github.com/greenbone/python-gvm/pull/133)
* Introduced Enum classes for authentication and privacy algorithms of SNMP
credentials [PR 133](https://github.com/greenbone/python-gvm/pull/133)
* Extended `InvalidArgument` and `RequiredArgument` errors to allow passing
argument and function name as keyword parameter [PR 134](https://github.com/greenbone/python-gvm/pull/134)

### Removed
* Removed hosts_ordering argument from `modify_target` [PR 88](https://github.com/greenbone/python-gvm/pull/88)
Expand Down
47 changes: 47 additions & 0 deletions gvm/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,58 @@ class InvalidArgument(GvmError):
"""Raised if an invalid argument/parameter is passed

Derives from :py:class:`GvmError`

Attributes:
message (str, optional): Error message to be displayed. Takes precedence
over argument and function
argument (str, optional): Optional name of the invalid argument
function (str, optional): Optional name of the called function
"""

def __init__(self, message=None, *, argument=None, function=None):
# pylint: disable=super-init-not-called
self.message = message
self.argument = argument
self.function = function

def __str__(self):
if self.message:
return self.message

if not self.function:
return "Invalid argument {}".format(self.argument)

if not self.argument:
return "Invalid argument for {}".format(self.function)

return "Invalid argument {} for {}".format(self.argument, self.function)


class RequiredArgument(GvmError):
"""Raised if a required argument/parameter is missing

Derives from :py:class:`GvmError`

Attributes:
message (str): Error message to be displayed
argument (str, optional): Optional name of the required argument
function (str, optional): Optional name of the called function
"""

def __init__(self, message=None, argument=None, function=None):
# pylint: disable=super-init-not-called
self.message = message
self.argument = argument
self.function = function

def __str__(self):
if self.message:
return self.message

if not self.function:
return "Required argument {}".format(self.argument)

if not self.argument:
return "Required argument missing for {}".format(self.function)

return "{} requires a {} argument".format(self.function, self.argument)
4 changes: 2 additions & 2 deletions gvm/protocols/gmpv7.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def import_config(self, config):
cmd.append_xml_str(config)
except etree.XMLSyntaxError as e:
raise InvalidArgument(
"Invalid xml passed as config to import_config", e
"Invalid xml passed as config to import_config {}".format(e)
)

return self._send_xml_command(cmd)
Expand Down Expand Up @@ -1291,7 +1291,7 @@ def import_report(
cmd.append_xml_str(report)
except etree.XMLSyntaxError as e:
raise InvalidArgument(
"Invalid xml passed as report to import_report", e
"Invalid xml passed as report to import_report {}".format(e)
)

return self._send_xml_command(cmd)
Expand Down
Loading