Browse Source

[2518] Update common DNS exceptions into a hierarchy

Mukund Sivaraman 11 years ago
parent
commit
ad3f1d33b9

+ 0 - 1
src/lib/dns/python/name_python.cc

@@ -545,7 +545,6 @@ PyObject* po_BadLabelType;
 PyObject* po_BadEscape;
 PyObject* po_IncompleteName;
 PyObject* po_InvalidBufferPosition;
-PyObject* po_DNSMessageFORMERR;
 
 //
 // Definition of enums

+ 0 - 1
src/lib/dns/python/name_python.h

@@ -31,7 +31,6 @@ extern PyObject* po_BadLabelType;
 extern PyObject* po_BadEscape;
 extern PyObject* po_IncompleteName;
 extern PyObject* po_InvalidBufferPosition;
-extern PyObject* po_DNSMessageFORMERR;
 
 //
 // Declaration of enums

+ 28 - 11
src/lib/dns/python/pydnspp.cc

@@ -28,6 +28,7 @@
 #include <Python.h>
 #include <structmember.h>
 
+#include <dns/exceptions.h>
 #include <dns/message.h>
 #include <dns/opcode.h>
 #include <dns/tsig.h>
@@ -152,8 +153,14 @@ initModulePart_Message(PyObject* mod) {
             PyErr_NewException("pydnspp.InvalidMessageUDPSize", NULL, NULL);
         PyObjectContainer(po_InvalidMessageUDPSize).installToModule(
             mod, "InvalidMessageUDPSize");
+        po_DNSMessageFORMERR =
+            PyErr_NewException("pydnspp.DNSMessageFORMERR",
+                               po_DNSProtocolError, NULL);
+        PyObjectContainer(po_DNSMessageFORMERR).installToModule(
+            mod, "DNSMessageFORMERR");
         po_DNSMessageBADVERS =
-            PyErr_NewException("pydnspp.DNSMessageBADVERS", NULL, NULL);
+            PyErr_NewException("pydnspp.DNSMessageBADVERS",
+                               po_DNSProtocolError, NULL);
         PyObjectContainer(po_DNSMessageBADVERS).installToModule(
             mod, "DNSMessageBADVERS");
         po_UnknownNSEC3HashAlgorithm =
@@ -245,7 +252,7 @@ initModulePart_Name(PyObject* mod) {
     try {
         po_NameParserException =
             PyErr_NewException("pydnspp.NameParserException",
-                               po_IscException, NULL);
+                               po_DNSTextError, NULL);
         PyObjectContainer(po_NameParserException)
             .installToModule(mod, "NameParserException");
 
@@ -277,13 +284,6 @@ initModulePart_Name(PyObject* mod) {
             PyErr_NewException("pydnspp.InvalidBufferPosition", NULL, NULL);
         PyObjectContainer(po_InvalidBufferPosition).installToModule(
             mod, "InvalidBufferPosition");
-
-        // This one could have gone into the message_python.cc file, but is
-        // already needed here.
-        po_DNSMessageFORMERR = PyErr_NewException("pydnspp.DNSMessageFORMERR",
-                                                  NULL, NULL);
-        PyObjectContainer(po_DNSMessageFORMERR).installToModule(
-            mod, "DNSMessageFORMERR");
     } catch (const std::exception& ex) {
         const std::string ex_what =
             "Unexpected failure in Name initialization: " +
@@ -878,12 +878,29 @@ PyInit_pydnspp(void) {
         po_InvalidOperation = PyErr_NewException("pydnspp.InvalidOperation",
                                                  po_IscException, NULL);
         PyObjectContainer(po_InvalidOperation)
-	    .installToModule(mod, "InvalidOperation");
+            .installToModule(mod, "InvalidOperation");
 
         po_InvalidParameter = PyErr_NewException("pydnspp.InvalidParameter",
                                                  po_IscException, NULL);
         PyObjectContainer(po_InvalidParameter)
-	    .installToModule(mod, "InvalidParameter");
+            .installToModule(mod, "InvalidParameter");
+
+        // Add DNS exceptions
+        po_DNSException = PyErr_NewException("pydnspp.DNSException",
+                                             po_IscException, NULL);
+        PyObjectContainer(po_DNSException)
+            .installToModule(mod, "DNSException");
+
+        po_DNSTextError = PyErr_NewException("pydnspp.DNSTextError",
+                                             po_DNSException, NULL);
+        PyObjectContainer(po_DNSTextError)
+            .installToModule(mod, "DNSTextError");
+
+        po_DNSProtocolError = PyErr_NewException("pydnspp.DNSProtocolError",
+                                             po_DNSException, NULL);
+        PyObjectContainer(po_DNSProtocolError)
+            .installToModule(mod, "DNSProtocolError");
+
     } catch (const std::exception& ex) {
         const std::string ex_what =
             "Unexpected failure in pydnspp initialization: " +

+ 5 - 1
src/lib/dns/python/pydnspp_common.cc

@@ -50,7 +50,11 @@ PyObject* po_IscException;
 PyObject* po_InvalidOperation;
 PyObject* po_InvalidParameter;
 
-// For our own isc::dns::Exception
+// For DNS exceptions
+PyObject* po_DNSException;
+PyObject* po_DNSTextError;
+PyObject* po_DNSProtocolError;
+PyObject* po_DNSMessageFORMERR;
 PyObject* po_DNSMessageBADVERS;
 
 

+ 4 - 0
src/lib/dns/python/pydnspp_common.h

@@ -32,6 +32,10 @@ extern PyObject* po_InvalidOperation;
 extern PyObject* po_InvalidParameter;
 
 // For our own isc::dns::Exception
+extern PyObject* po_DNSException;
+extern PyObject* po_DNSTextError;
+extern PyObject* po_DNSProtocolError;
+extern PyObject* po_DNSMessageFORMERR;
 extern PyObject* po_DNSMessageBADVERS;
 
 // This function reads 'bytes' from a sequence

+ 1 - 0
src/lib/dns/python/rdata_python.cc

@@ -25,6 +25,7 @@
 #include "rrclass_python.h"
 #include "messagerenderer_python.h"
 #include "name_python.h"
+#include "pydnspp_common.h"
 
 using namespace isc::dns;
 using namespace isc::dns::python;

+ 2 - 0
src/lib/dns/python/tests/name_python_test.py

@@ -105,6 +105,8 @@ class NameTest(unittest.TestCase):
         self.assertTrue(isinstance(TooLongName(), NameParserException))
         self.assertTrue(isinstance(IncompleteName(), NameParserException))
 
+        self.assertTrue(isinstance(NameParserException(), DNSTextError))
+
     def test_at(self):
         self.assertEqual(7, self.name1.at(0))
         self.assertEqual(101, self.name1.at(1))

+ 4 - 0
src/lib/dns/python/tests/pydnspp_python_test.py

@@ -26,5 +26,9 @@ class CommonTest(unittest.TestCase):
         self.assertTrue(isinstance(InvalidOperation(), IscException))
         self.assertTrue(isinstance(InvalidParameter(), IscException))
 
+        self.assertTrue(isinstance(DNSException(), IscException))
+        self.assertTrue(isinstance(DNSTextError(), DNSException))
+        self.assertTrue(isinstance(DNSProtocolError(), DNSException))
+
 if __name__ == '__main__':
     unittest.main()