diff --git a/ci/testsuite-result.json b/ci/testsuite-result.json index b176806b..62f3faf5 100644 --- a/ci/testsuite-result.json +++ b/ci/testsuite-result.json @@ -6,7 +6,6 @@ "command_issued": "zone create example.org hostmaster@example.org ns1.example.org # should require force because ns1 is unknown", "ok": [], "warning": [ - "Host ns1.example.org not found.", "ns1.example.org is not in mreg, must force" ], "error": [], @@ -41,9 +40,7 @@ "ok": [ "Created zone example.org" ], - "warning": [ - "Host ns1.example.org not found." - ], + "warning": [], "error": [], "output": [], "api_requests": [ @@ -210,10 +207,9 @@ "command": "zone set_ns example.org ns2.example.org", "command_filter": null, "command_filter_negate": false, - "command_issued": "zone set_ns example.org ns2.example.org # requires force because ns2 is unknown", + "command_issued": "zone set_ns example.org ns2.example.org # requires force because ns2 is unknown", "ok": [], "warning": [ - "Host ns2.example.org not found.", "ns2.example.org is not in mreg, must force" ], "error": [], @@ -271,9 +267,7 @@ "ok": [ "Updated nameservers for example.org" ], - "warning": [ - "Host ns2.example.org not found." - ], + "warning": [], "error": [], "output": [], "api_requests": [ @@ -15283,9 +15277,7 @@ "ok": [ "Created zone delegation wut.example.org" ], - "warning": [ - "Host ns2.example.org not found." - ], + "warning": [], "error": [], "output": [], "api_requests": [ diff --git a/mreg_cli/cli.py b/mreg_cli/cli.py index 0e69a788..f8aa934d 100644 --- a/mreg_cli/cli.py +++ b/mreg_cli/cli.py @@ -167,7 +167,7 @@ def parse(self, command: str) -> None: self.last_errno = e.code except (CliWarning, CliError) as exc: - exc.print_self() + exc.print_and_log() except CliExit: # If we have active recordings going on, save them before exiting @@ -263,7 +263,7 @@ def process_command_line(self, line: str) -> None: try: cmd = output.from_command(line) except (CliWarning, CliError) as exc: - exc.print_self() + exc.print_and_log() return # Create and set the corrolation id, using the cleaned command # as the suffix. This is used to track the command in the logs diff --git a/mreg_cli/exceptions.py b/mreg_cli/exceptions.py index 4d5095d6..e8b29323 100644 --- a/mreg_cli/exceptions.py +++ b/mreg_cli/exceptions.py @@ -16,6 +16,9 @@ from prompt_toolkit.formatted_text.html import html_escape +logger = logging.getLogger(__name__) + + class CliExit(Exception): """Exception used to exit the CLI.""" @@ -37,10 +40,22 @@ def formatted_exception(self) -> str: # NOTE: override this in subclasses to provide custom formatting. return self.escape() + def log(self): + """Log the exception.""" + from mreg_cli.outputmanager import OutputManager + + logger.error(str(self)) + OutputManager().add_error(str(self)) + def print_self(self): """Print the exception with appropriate formatting.""" print_formatted_text(HTML(self.formatted_exception()), file=sys.stdout) + def print_and_log(self): + """Print the exception and log it.""" + self.log() + self.print_self() + class CliError(CliException): """Exception class for CLI errors. @@ -49,14 +64,6 @@ class CliError(CliException): the user cannot be expected to resolve. """ - def __init__(self, *args: Any, **kwargs: Any): - """Initialize the error.""" - super().__init__(*args, **kwargs) - logging.getLogger(__name__).error(str(self)) - from mreg_cli.outputmanager import OutputManager - - OutputManager().add_error(str(self)) - def formatted_exception(self) -> str: """Return a string formatted with HTML red tag for the error message. @@ -71,12 +78,11 @@ class CliWarning(CliException): Warnings should be recoverable by changing the user input. """ - def __init__(self, *args: Any, **kwargs: Any): - """Initialize the warning.""" + def log(self): + """Log the exception.""" from mreg_cli.outputmanager import OutputManager - super().__init__(*args, **kwargs) - logging.getLogger(__name__).warning(str(self)) + logger.warning(str(self)) OutputManager().add_warning(str(self)) def formatted_exception(self) -> str: diff --git a/mreg_cli/main.py b/mreg_cli/main.py index ceaa6b87..2018bd46 100644 --- a/mreg_cli/main.py +++ b/mreg_cli/main.py @@ -154,7 +154,7 @@ def main(): ) except (EOFError, KeyboardInterrupt, LoginFailedError) as e: if isinstance(e, LoginFailedError): - e.print_self() + e.print_and_log() else: print(e) raise SystemExit() from None @@ -204,7 +204,7 @@ def main(): except EOFError: raise SystemExit() from None except CliException as e: - e.print_self() + e.print_and_log() raise SystemExit() from None else: try: diff --git a/mreg_cli/utilities/api.py b/mreg_cli/utilities/api.py index 6e0cc154..0d522772 100644 --- a/mreg_cli/utilities/api.py +++ b/mreg_cli/utilities/api.py @@ -145,7 +145,7 @@ def prompt_for_password_and_login(user: str, url: str, catch_exception: bool = T auth_and_update_token(user, password) except CliError as e: if catch_exception: - e.print_self() + e.print_and_log() else: raise LoginFailedError("Updating token failed.") from e @@ -169,7 +169,7 @@ def prompt_for_password_and_try_update_token() -> None: raise LoginFailedError("Unable to determine username.") auth_and_update_token(user, password) except CliError as e: - e.print_self() + e.print_and_log() def auth_and_update_token(username: str, password: str) -> None: