Skip to content

Commit 6309fcf

Browse files
committed
Convert NoNameservers exception to the new DNSException style.
str() for parametrized version now returns string like: All nameservers failed to answer the query localhost. IN URI: Server 192.0.2.1 anwered BADVERS; Server 192.0.2.55 anwered BADVERS
1 parent bc87a56 commit 6309fcf

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

dns/resolver.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,24 @@ def _fmt_kwargs(self, **kwargs):
8181
query=kwargs['response'].question)
8282

8383
class NoNameservers(dns.exception.DNSException):
84-
"""No non-broken nameservers are available to answer the query."""
85-
def __init__(self, errors=[]):
86-
"""Optionally construct message with list of servers and errors.
84+
"""All nameservers failed to answer the query.
8785
88-
@param errors: list of servers and respective errors
89-
@type errors: [(server ip address, any object convertible to string)]
90-
"""
91-
super(dns.exception.DNSException, self).__init__()
92-
self.errors = errors
86+
@param errors: list of servers and respective errors
87+
@type errors: [(server ip address, any object convertible to string)]
88+
Non-empty errors list will add explanatory message ()
89+
"""
90+
91+
msg = "All nameservers failed to answer the query."
92+
fmt = "%s {query}: {errors}" % msg[:-1]
93+
supp_kwargs = set(['query', 'errors'])
94+
95+
def _fmt_kwargs(self, **kwargs):
96+
srv_msgs = []
97+
for err in kwargs['errors']:
98+
srv_msgs.append('Server %s anwered %s' % (err[0], err[1]))
99+
return super(NoNameservers, self)._fmt_kwargs(query=kwargs['query'],
100+
errors='; '.join(srv_msgs))
93101

94-
def __str__(self):
95-
message = self.__doc__
96-
if self.errors:
97-
srv_msgs = []
98-
for err in self.errors:
99-
srv_msgs.append('Server %s %s' % (err[0], err[1]))
100-
message += ' %s' % '; '.join(srv_msgs)
101-
return message
102102

103103
class NotAbsolute(dns.exception.DNSException):
104104
"""An absolute domain name is required but a relative name was provided."""
@@ -851,7 +851,7 @@ def query(self, qname, rdtype=dns.rdatatype.A, rdclass=dns.rdataclass.IN,
851851
backoff = 0.10
852852
while response is None:
853853
if len(nameservers) == 0:
854-
raise NoNameservers(errors)
854+
raise NoNameservers(query=request.question, errors=errors)
855855
for nameserver in nameservers[:]:
856856
timeout = self._compute_timeout(start)
857857
try:

0 commit comments

Comments
 (0)