Skip to content

Commit

Permalink
Handle more exceptions, wrap runtime_error in DublinTracerouteException
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Barberio <[email protected]>
  • Loading branch information
insomniacslk committed Aug 29, 2024
1 parent 94d98ef commit 5f8b4ee
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions dublintraceroute/py3/_dublintraceroute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ typedef struct {
} DublinTracerouteClass;


static PyObject *PyDublinTracerouteException = NULL;

static PyModuleDef _dublintraceroutemodule = {
PyModuleDef_HEAD_INIT,
"_dublintraceroute",
Expand Down Expand Up @@ -102,12 +104,16 @@ static PyObject* DublinTraceroute_traceroute(PyObject *self, PyObject *args)
} catch (Tins::malformed_packet &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
return NULL;
} catch (std::runtime_error &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
return NULL;
} catch (DublinTracerouteException &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
PyErr_SetString(PyDublinTracerouteException, e.what());
return NULL;
} catch (std::invalid_argument &e) {
PyErr_SetString(PyExc_ValueError, e.what());
} catch (std::out_of_range &e) {
PyErr_SetString(PyExc_IndexError, e.what());
} catch (std::runtime_error &e) {
PyErr_SetString(PyExc_RuntimeError, e.what());
}

PyObject *py_results = PyUnicode_FromString(results->to_json().c_str());
Expand Down Expand Up @@ -245,6 +251,13 @@ PyInit__dublintraceroute(void)
PyModule_AddObject(module, "DublinTraceroute",
(PyObject *)&DublinTracerouteType);

PyDublinTracerouteException = PyErr_NewException("_dublintraceroute.DublinTracerouteException", NULL, NULL);
if (PyDublinTracerouteException == NULL) {
return NULL;
}
Py_INCREF(&PyDublinTracerouteException);
PyModule_AddObject(module, "DublinTracerouteException", PyDublinTracerouteException);

/* export the default attributes */
PyObject_SetAttrString(module, "DEFAULT_SPORT",
PyLong_FromLong(DublinTraceroute::default_srcport));
Expand Down

0 comments on commit 5f8b4ee

Please sign in to comment.