Menu

#24 Raise subclasses of DNSError for more granular exc. handling

open
nobody
None
5
2011-08-07
2011-08-07
No

Currently pydns only ever throws a single exception class, DNSError. It, however, specifies varying string arguments depending on the actual error condition. Of course this is not very helpful to calling code because inspecting a textual message on an exception object is the exception handling equivalent of reading tea leaves. So anyone wanting not to get bad karma is stuck with generically catching DNSError.

I'm attaching a patch for pydns that defines a small hierarchy of exception classes based on DNSError to provide more granularity. This is 100% compatible with existing code using pydns as long as it doesn't explicitly look at exceptions' type or their message attribute; it breaks existing code that compares the type of an exception to DNS.DNSError instead of saying "except DNS.DNSError"; it also breaks code that requires the exception's message attribute to read "no working nameservers found" when there were errors for *all* of the configured nameservers.

The latter issue is caused by the patch changing send{UDP,TCP}Request to preserve the error that occurred with the first configured nameserver and raise that as a SocketError instead of the mostly useless "no working nameservers found" exception. All this will still work with existing code because SocketError is a subclass of DNSError — as long as the calling code doesn't depend on the exception's message attribute. However, if you think that's not an acceptable limitation of backwards compatibility, feel free to define a NoWorkingNameserversError exception and raise that, or just raise the basic DNSError.

The patch also changes DNS.lazy.dnslookup to raise ServerError (also a DNSError subclass) exceptions, which makes the RCODE available to calling code through an rcode attribute.

(Finally, the patch fixes the broken shebang lines in the test scripts.)

Discussion

  • Julian Mehnle

    Julian Mehnle - 2011-08-07

    Granular exceptions for pydns 2.3.5

     
  • Julian Mehnle

    Julian Mehnle - 2011-08-07

    Granular exceptions for pydns 3.0.1 RC1

     
  • Julian Mehnle

    Julian Mehnle - 2011-08-07

    And here's an equivalent patch for py3dns, against 3.0.1 RC1.

     
  • Stuart D. Gathman

    Applied to CVS. Sure wish I could update patch status.