]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
samba-tool dns: add a wrapper for better error messages
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Fri, 12 Aug 2022 04:38:59 +0000 (16:38 +1200)
committerDouglas Bagnall <dbagnall@samba.org>
Tue, 6 Sep 2022 21:12:36 +0000 (21:12 +0000)
This will help turn simple common errors into CommandError messages.

At this stage, no messages are intercepted.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/netcmd/dns.py

index 7c4d3f9bc6a17ea5d1235eb387d463cf7e70f0af..f3a5915b17ed36b1f5b4fd6d76ea498f3ecb966f 100644 (file)
@@ -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'