]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Extend NoAnswer exception with optional question.
authorPetr Spacek <pspacek@redhat.com>
Thu, 15 Jan 2015 16:44:49 +0000 (17:44 +0100)
committerPetr Spacek <pspacek@redhat.com>
Thu, 12 Feb 2015 11:38:31 +0000 (12:38 +0100)
The actual question will be printed as part of string representation of
NoAnswer exception instead of terse
"The response did not contain an answer to the question."

dns/resolver.py

index d59f413ab0018e1b3bc7859ca19858cbff91776d..93fb36d032c523f801a6a6f40841df80e889a21c 100644 (file)
@@ -61,8 +61,19 @@ class YXDOMAIN(dns.exception.DNSException):
 Timeout = dns.exception.Timeout
 
 class NoAnswer(dns.exception.DNSException):
-    """The response did not contain an answer to the question."""
-    pass
+    """The DNS response does not contain an answer to the question."""
+    def __init__(self, question=None):
+        super(dns.exception.DNSException, self).__init__()
+        self.question = question
+
+    def __str__(self):
+        message = self.__doc__
+        if self.question:
+            message = message[0:-1]
+            for q in self.question:
+                message += ' %s' % q
+        return message
+
 
 class NoNameservers(dns.exception.DNSException):
     """No non-broken nameservers are available to answer the query."""
@@ -139,11 +150,11 @@ class Answer(object):
                         continue
                     except KeyError:
                         if raise_on_no_answer:
-                            raise NoAnswer
+                            raise NoAnswer(question=response.question)
                 if raise_on_no_answer:
-                    raise NoAnswer
+                    raise NoAnswer(question=response.question)
         if rrset is None and raise_on_no_answer:
-            raise NoAnswer
+            raise NoAnswer(question=response.question)
         self.canonical_name = qname
         self.rrset = rrset
         if rrset is None: