Skip to content

Commit c3caf1a

Browse files
authored
Merge branch 'master' into fix-host-ipv6-lookup
2 parents 3879271 + 8392aa7 commit c3caf1a

File tree

5 files changed

+28
-30
lines changed

5 files changed

+28
-30
lines changed

ci/testsuite-result.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"command_issued": "zone create example.org hostmaster@example.org ns1.example.org # should require force because ns1 is unknown",
77
"ok": [],
88
"warning": [
9-
"Host ns1.example.org not found.",
109
"ns1.example.org is not in mreg, must force"
1110
],
1211
"error": [],
@@ -41,9 +40,7 @@
4140
"ok": [
4241
"Created zone example.org"
4342
],
44-
"warning": [
45-
"Host ns1.example.org not found."
46-
],
43+
"warning": [],
4744
"error": [],
4845
"output": [],
4946
"api_requests": [
@@ -210,10 +207,9 @@
210207
"command": "zone set_ns example.org ns2.example.org",
211208
"command_filter": null,
212209
"command_filter_negate": false,
213-
"command_issued": "zone set_ns example.org ns2.example.org # requires force because ns2 is unknown",
210+
"command_issued": "zone set_ns example.org ns2.example.org # requires force because ns2 is unknown",
214211
"ok": [],
215212
"warning": [
216-
"Host ns2.example.org not found.",
217213
"ns2.example.org is not in mreg, must force"
218214
],
219215
"error": [],
@@ -271,9 +267,7 @@
271267
"ok": [
272268
"Updated nameservers for example.org"
273269
],
274-
"warning": [
275-
"Host ns2.example.org not found."
276-
],
270+
"warning": [],
277271
"error": [],
278272
"output": [],
279273
"api_requests": [
@@ -15283,9 +15277,7 @@
1528315277
"ok": [
1528415278
"Created zone delegation wut.example.org"
1528515279
],
15286-
"warning": [
15287-
"Host ns2.example.org not found."
15288-
],
15280+
"warning": [],
1528915281
"error": [],
1529015282
"output": [],
1529115283
"api_requests": [

mreg_cli/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def parse(self, command: str) -> None:
167167
self.last_errno = e.code
168168

169169
except (CliWarning, CliError) as exc:
170-
exc.print_self()
170+
exc.print_and_log()
171171

172172
except CliExit:
173173
# If we have active recordings going on, save them before exiting
@@ -263,7 +263,7 @@ def process_command_line(self, line: str) -> None:
263263
try:
264264
cmd = output.from_command(line)
265265
except (CliWarning, CliError) as exc:
266-
exc.print_self()
266+
exc.print_and_log()
267267
return
268268
# Create and set the corrolation id, using the cleaned command
269269
# as the suffix. This is used to track the command in the logs

mreg_cli/exceptions.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from prompt_toolkit.formatted_text.html import html_escape
1717

1818

19+
logger = logging.getLogger(__name__)
20+
21+
1922
class CliExit(Exception):
2023
"""Exception used to exit the CLI."""
2124

@@ -37,10 +40,22 @@ def formatted_exception(self) -> str:
3740
# NOTE: override this in subclasses to provide custom formatting.
3841
return self.escape()
3942

43+
def log(self):
44+
"""Log the exception."""
45+
from mreg_cli.outputmanager import OutputManager
46+
47+
logger.error(str(self))
48+
OutputManager().add_error(str(self))
49+
4050
def print_self(self):
4151
"""Print the exception with appropriate formatting."""
4252
print_formatted_text(HTML(self.formatted_exception()), file=sys.stdout)
4353

54+
def print_and_log(self):
55+
"""Print the exception and log it."""
56+
self.log()
57+
self.print_self()
58+
4459

4560
class CliError(CliException):
4661
"""Exception class for CLI errors.
@@ -49,14 +64,6 @@ class CliError(CliException):
4964
the user cannot be expected to resolve.
5065
"""
5166

52-
def __init__(self, *args: Any, **kwargs: Any):
53-
"""Initialize the error."""
54-
super().__init__(*args, **kwargs)
55-
logging.getLogger(__name__).error(str(self))
56-
from mreg_cli.outputmanager import OutputManager
57-
58-
OutputManager().add_error(str(self))
59-
6067
def formatted_exception(self) -> str:
6168
"""Return a string formatted with HTML red tag for the error message.
6269
@@ -71,12 +78,11 @@ class CliWarning(CliException):
7178
Warnings should be recoverable by changing the user input.
7279
"""
7380

74-
def __init__(self, *args: Any, **kwargs: Any):
75-
"""Initialize the warning."""
81+
def log(self):
82+
"""Log the exception."""
7683
from mreg_cli.outputmanager import OutputManager
7784

78-
super().__init__(*args, **kwargs)
79-
logging.getLogger(__name__).warning(str(self))
85+
logger.warning(str(self))
8086
OutputManager().add_warning(str(self))
8187

8288
def formatted_exception(self) -> str:

mreg_cli/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def main():
154154
)
155155
except (EOFError, KeyboardInterrupt, LoginFailedError) as e:
156156
if isinstance(e, LoginFailedError):
157-
e.print_self()
157+
e.print_and_log()
158158
else:
159159
print(e)
160160
raise SystemExit() from None
@@ -204,7 +204,7 @@ def main():
204204
except EOFError:
205205
raise SystemExit() from None
206206
except CliException as e:
207-
e.print_self()
207+
e.print_and_log()
208208
raise SystemExit() from None
209209
else:
210210
try:

mreg_cli/utilities/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def prompt_for_password_and_login(user: str, url: str, catch_exception: bool = T
145145
auth_and_update_token(user, password)
146146
except CliError as e:
147147
if catch_exception:
148-
e.print_self()
148+
e.print_and_log()
149149
else:
150150
raise LoginFailedError("Updating token failed.") from e
151151

@@ -169,7 +169,7 @@ def prompt_for_password_and_try_update_token() -> None:
169169
raise LoginFailedError("Unable to determine username.")
170170
auth_and_update_token(user, password)
171171
except CliError as e:
172-
e.print_self()
172+
e.print_and_log()
173173

174174

175175
def auth_and_update_token(username: str, password: str) -> None:

0 commit comments

Comments
 (0)