From: Douglas Bagnall Date: Fri, 12 Aug 2022 04:38:59 +0000 (+1200) Subject: samba-tool dns: add a wrapper for better error messages X-Git-Tag: talloc-2.4.0~1175 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=26b86bc57e85280a9fc9aba26a49a16859c91b78;p=thirdparty%2Fsamba.git samba-tool dns: add a wrapper for better error messages This will help turn simple common errors into CommandError messages. At this stage, no messages are intercepted. Signed-off-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/netcmd/dns.py b/python/samba/netcmd/dns.py index 7c4d3f9bc6a..f3a5915b17e 100644 --- a/python/samba/netcmd/dns.py +++ b/python/samba/netcmd/dns.py @@ -56,6 +56,44 @@ def dns_connect(server, lp, creds): return dns_conn +class DnsConnWrapper: + """A wrapper around a dnsserver.dnsserver connection that makes it + harder not to report friendly messages. + """ + + default_messages = { + } + + def __init__(self, server, lp, creds): + self.dns_conn = dns_connect(server, lp, creds) + + def __getattr__(self, name): + attr = getattr(self.dns_conn, name) + if name not in { + "DnssrvComplexOperation2", + "DnssrvEnumRecords2", + "DnssrvOperation2", + "DnssrvQuery2", + "DnssrvUpdateRecord2"}: + return attr + + def f(*args, messages={}): + try: + return attr(*args) + except WERRORError as e: + werr, errstr = e.args + if werr in messages: + if werr is None: + # None overrides a default message, leaving the bare exception + raise + raise CommandError(f"{messages[werr]} [{errstr}]", e) + if werr in self.default_messages: + raise CommandError(f"{self.default_messages[werr]} [{errstr}]", e) + raise + + return f + + def bool_string(flag): if flag == 0: ret = 'FALSE'