From: Petr Spacek Date: Thu, 15 Jan 2015 16:44:49 +0000 (+0100) Subject: Extend NoAnswer exception with optional question. X-Git-Tag: v1.13.0~35^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=13f4609d811336361cd38b1f1a3d519f2d603317;p=thirdparty%2Fdnspython.git Extend NoAnswer exception with optional question. 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." --- diff --git a/dns/resolver.py b/dns/resolver.py index 79de3ab4..ca9f98f3 100644 --- a/dns/resolver.py +++ b/dns/resolver.py @@ -58,8 +58,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.""" @@ -136,11 +147,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: