Browse Source

[2004] Catch std::exception before ... to print some detail if possible

Also update the messages returned.
Mukund Sivaraman 13 years ago
parent
commit
c28fd5d9f5
1 changed files with 97 additions and 89 deletions
  1. 97 89
      src/lib/dns/python/message_python.cc

+ 97 - 89
src/lib/dns/python/message_python.cc

@@ -219,12 +219,14 @@ Message_getHeaderFlag(s_Message* self, PyObject* args) {
     } catch (const isc::InvalidParameter& ip) {
     } catch (const isc::InvalidParameter& ip) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidParameter, ip.what());
         PyErr_SetString(po_InvalidParameter, ip.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_header_flag(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in getHeaderFlag");
-        return (NULL);
+                        "Unexpected exception in Message.get_header_flag()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -250,16 +252,17 @@ Message_setHeaderFlag(s_Message* self, PyObject* args) {
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::InvalidParameter& ip) {
     } catch (const isc::InvalidParameter& ip) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidParameter, ip.what());
         PyErr_SetString(po_InvalidParameter, ip.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_header_flag(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setHeaderFlag");
-        return (NULL);
+                        "Unexpected exception in Message.set_header_flag()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -287,12 +290,14 @@ Message_setQid(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_qid(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setQid");
-        return (NULL);
+                        "Unexpected exception in Message.set_qid()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -301,11 +306,14 @@ Message_getRcode(s_Message* self) {
         return (createRcodeObject(self->cppobj->getRcode()));
         return (createRcodeObject(self->cppobj->getRcode()));
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_rcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_rcode()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -319,12 +327,14 @@ Message_setRcode(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_rcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setRcode");
-        return (NULL);
+                        "Unexpected exception in Message.set_rcode()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -333,17 +343,14 @@ Message_getOpcode(s_Message* self) {
         return (createOpcodeObject(self->cppobj->getOpcode()));
         return (createOpcodeObject(self->cppobj->getOpcode()));
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const exception& ex) {
     } catch (const exception& ex) {
-        const string ex_what =
-            "Failed to get message opcode: " + string(ex.what());
+        const string ex_what = "Error in Message.get_opcode(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
         PyErr_SetString(po_IscException, ex_what.c_str());
-        return (NULL);
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception getting opcode from message");
-        return (NULL);
+                        "Unexpected exception in Message.get_opcode()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -357,12 +364,14 @@ Message_setOpcode(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_opcode(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setOpcode");
-        return (NULL);
+                        "Unexpected exception in Message.set_opcode()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -374,12 +383,11 @@ Message_getEDNS(s_Message* self) {
     try {
     try {
         return (createEDNSObject(*src));
         return (createEDNSObject(*src));
     } catch (const exception& ex) {
     } catch (const exception& ex) {
-        const string ex_what =
-            "Failed to get EDNS from message: " + string(ex.what());
+        const string ex_what = "Error in Message.get_edns(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(PyExc_SystemError,
         PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure getting EDNS from message");
+                        "Unexpected exception in Message.get_edns()");
     }
     }
     return (NULL);
     return (NULL);
 }
 }
@@ -395,12 +403,14 @@ Message_setEDNS(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.set_edns(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in setEDNS");
-        return (NULL);
+                        "Unexpected exception in Message.set_edns()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -416,13 +426,11 @@ Message_getTSIGRecord(s_Message* self) {
     } catch (const InvalidMessageOperation& ex) {
     } catch (const InvalidMessageOperation& ex) {
         PyErr_SetString(po_InvalidMessageOperation, ex.what());
         PyErr_SetString(po_InvalidMessageOperation, ex.what());
     } catch (const exception& ex) {
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in getting TSIGRecord from message: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_tsig_record(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError, "Unexpected failure in "
-                        "getting TSIGRecord from message");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_tsig_record()");
     }
     }
     return (NULL);
     return (NULL);
 }
 }
@@ -441,12 +449,14 @@ Message_getRRCount(s_Message* self, PyObject* args) {
                                   static_cast<Message::Section>(section))));
                                   static_cast<Message::Section>(section))));
     } catch (const isc::OutOfRange& ex) {
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.get_rr_count(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in getRRCount");
-        return (NULL);
+                        "Unexpected exception in Message.get_rr_count()");
     }
     }
+    return (NULL);
 }
 }
 
 
 // This is a helper templated class commonly used for getQuestion and
 // This is a helper templated class commonly used for getQuestion and
@@ -487,13 +497,11 @@ Message_getQuestion(PyObject* po_self, PyObject*) {
     } catch (const InvalidMessageSection& ex) {
     } catch (const InvalidMessageSection& ex) {
         PyErr_SetString(po_InvalidMessageSection, ex.what());
         PyErr_SetString(po_InvalidMessageSection, ex.what());
     } catch (const exception& ex) {
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in Message.get_question: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_question(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure in Message.get_question");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_question()");
     }
     }
     return (NULL);
     return (NULL);
 }
 }
@@ -523,13 +531,11 @@ Message_getSection(PyObject* po_self, PyObject* args) {
     } catch (const InvalidMessageSection& ex) {
     } catch (const InvalidMessageSection& ex) {
         PyErr_SetString(po_InvalidMessageSection, ex.what());
         PyErr_SetString(po_InvalidMessageSection, ex.what());
     } catch (const exception& ex) {
     } catch (const exception& ex) {
-        const string ex_what =
-            "Unexpected failure in Message.get_section: " +
-            string(ex.what());
+        const string ex_what = "Error in Message.get_section(): " + string(ex.what());
         PyErr_SetString(po_IscException, ex_what.c_str());
         PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(PyExc_SystemError,
-                        "Unexpected failure in Message.get_section");
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.get_section()");
     }
     }
     return (NULL);
     return (NULL);
 }
 }
@@ -553,12 +559,14 @@ Message_addQuestion(s_Message* self, PyObject* args) {
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.add_question(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in addQuestion");
-        return (NULL);
+                        "Unexpected exception in Message.add_question()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -577,15 +585,16 @@ Message_addRRset(s_Message* self, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::OutOfRange& ex) {
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.add_rrset(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in adding RRset");
-        return (NULL);
+                        "Unexpected exception in Message.add_rrset()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -607,9 +616,12 @@ Message_clear(s_Message* self, PyObject* args) {
                 return (NULL);
                 return (NULL);
             }
             }
         }
         }
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.clear(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception when clearing message");
+                        "Unexpected exception in Message.clear()");
     }
     }
     return (NULL);
     return (NULL);
 }
 }
@@ -627,15 +639,16 @@ Message_clearSection(PyObject* pyself, PyObject* args) {
         Py_RETURN_NONE;
         Py_RETURN_NONE;
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
     } catch (const isc::OutOfRange& ex) {
     } catch (const isc::OutOfRange& ex) {
         PyErr_SetString(PyExc_OverflowError, ex.what());
         PyErr_SetString(PyExc_OverflowError, ex.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.clear_section(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
         PyErr_SetString(po_IscException,
         PyErr_SetString(po_IscException,
-                        "Unexpected exception in clearSection");
-        return (NULL);
+                        "Unexpected exception in Message.clear_section()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -646,11 +659,14 @@ Message_makeResponse(s_Message* self) {
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.make_response(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception in Message.makeResponse");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.make_response()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -661,11 +677,14 @@ Message_toText(s_Message* self) {
     } catch (const InvalidMessageOperation& imo) {
     } catch (const InvalidMessageOperation& imo) {
         PyErr_Clear();
         PyErr_Clear();
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
         PyErr_SetString(po_InvalidMessageOperation, imo.what());
-        return (NULL);
+    } catch (const exception& ex) {
+        const string ex_what = "Error in Message.to_text(): " + string(ex.what());
+        PyErr_SetString(po_IscException, ex_what.c_str());
     } catch (...) {
     } catch (...) {
-        PyErr_SetString(po_IscException, "Unexpected exception");
-        return (NULL);
+        PyErr_SetString(po_IscException,
+                        "Unexpected exception in Message.to_text()");
     }
     }
+    return (NULL);
 }
 }
 
 
 PyObject*
 PyObject*
@@ -696,22 +715,18 @@ Message_toWire(s_Message* self, PyObject* args) {
         } catch (const InvalidMessageOperation& imo) {
         } catch (const InvalidMessageOperation& imo) {
             PyErr_Clear();
             PyErr_Clear();
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
-            return (NULL);
         } catch (const TSIGContextError& ex) {
         } catch (const TSIGContextError& ex) {
             // toWire() with a TSIG context can fail due to this if the
             // toWire() with a TSIG context can fail due to this if the
             // python program has a bug.
             // python program has a bug.
             PyErr_SetString(po_TSIGContextError, ex.what());
             PyErr_SetString(po_TSIGContextError, ex.what());
-            return (NULL);
-        } catch (const std::exception& ex) {
-            // Other exceptions should be rare (most likely an implementation
-            // bug)
-            PyErr_SetString(po_TSIGContextError, ex.what());
-            return (NULL);
+        } catch (const exception& ex) {
+            const string ex_what = "Error in Message.to_wire(): " + string(ex.what());
+            PyErr_SetString(po_TSIGContextError, ex_what.c_str());
         } catch (...) {
         } catch (...) {
-            PyErr_SetString(PyExc_RuntimeError,
-                            "Unexpected C++ exception in Message.to_wire");
-            return (NULL);
+            PyErr_SetString(po_IscException,
+                            "Unexpected exception in Message.to_wire()");
         }
         }
+        return (NULL);
     }
     }
     PyErr_Clear();
     PyErr_Clear();
     PyErr_SetString(PyExc_TypeError,
     PyErr_SetString(PyExc_TypeError,
@@ -739,29 +754,22 @@ Message_fromWire(PyObject* pyself, PyObject* args) {
             Py_RETURN_NONE;
             Py_RETURN_NONE;
         } catch (const InvalidMessageOperation& imo) {
         } catch (const InvalidMessageOperation& imo) {
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
             PyErr_SetString(po_InvalidMessageOperation, imo.what());
-            return (NULL);
         } catch (const DNSMessageFORMERR& dmfe) {
         } catch (const DNSMessageFORMERR& dmfe) {
             PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
             PyErr_SetString(po_DNSMessageFORMERR, dmfe.what());
-            return (NULL);
         } catch (const DNSMessageBADVERS& dmfe) {
         } catch (const DNSMessageBADVERS& dmfe) {
             PyErr_SetString(po_DNSMessageBADVERS, dmfe.what());
             PyErr_SetString(po_DNSMessageBADVERS, dmfe.what());
-            return (NULL);
         } catch (const MessageTooShort& mts) {
         } catch (const MessageTooShort& mts) {
             PyErr_SetString(po_MessageTooShort, mts.what());
             PyErr_SetString(po_MessageTooShort, mts.what());
-            return (NULL);
         } catch (const InvalidBufferPosition& ex) {
         } catch (const InvalidBufferPosition& ex) {
             PyErr_SetString(po_DNSMessageFORMERR, ex.what());
             PyErr_SetString(po_DNSMessageFORMERR, ex.what());
-            return (NULL);
         } catch (const exception& ex) {
         } catch (const exception& ex) {
-            const string ex_what =
-                "Error in Message.from_wire: " + string(ex.what());
-            PyErr_SetString(PyExc_RuntimeError, ex_what.c_str());
-            return (NULL);
+            const string ex_what = "Error in Message.from_wire(): " + string(ex.what());
+            PyErr_SetString(po_IscException, ex_what.c_str());
         } catch (...) {
         } catch (...) {
-            PyErr_SetString(PyExc_RuntimeError,
-                            "Unexpected exception in Message.from_wire");
-            return (NULL);
+            PyErr_SetString(po_IscException,
+                            "Unexpected exception in Message.from_wire()");
         }
         }
+        return (NULL);
     }
     }
 
 
     PyErr_SetString(PyExc_TypeError,
     PyErr_SetString(PyExc_TypeError,