From d7abfae1ed7d8e977b2567db0384e80cc8ce09f1 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sun, 20 Oct 2013 18:46:41 -0700 Subject: [PATCH] Cleanup exception handling --- capnp/capabilityHelper.h | 32 +++++++++++--------------------- test/test_capability.py | 4 ++-- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/capnp/capabilityHelper.h b/capnp/capabilityHelper.h index b81a50a..0c9023a 100644 --- a/capnp/capabilityHelper.h +++ b/capnp/capabilityHelper.h @@ -10,28 +10,26 @@ extern "C" { PyObject * wrap_kj_exception(kj::Exception &); } +void check_py_error() { + PyObject * err = PyErr_Occurred(); + if(err) { + // PyErr_Clear(); + throw std::exception(); + } +} + PyObject * wrapPyFunc(PyObject * func, PyObject * arg) { PyObject * result = PyObject_CallFunctionObjArgs(func, arg, NULL); Py_DECREF(func); - PyObject * err = PyErr_Occurred(); - if(err) { - char * errorMsg = PyString_AsString(PyObject_Repr(err)); - // PyErr_Clear(); - throw std::invalid_argument(errorMsg); - } + check_py_error(); return result; } void wrapRemoteCall(PyObject * func, capnp::Response & arg) { wrap_remote_call(func, arg); - PyObject * err = PyErr_Occurred(); - if(err) { - char * errorMsg = PyString_AsString(PyObject_Repr(err)); - // PyErr_Clear(); - throw std::invalid_argument(errorMsg); - } + check_py_error(); } ::kj::Promise evalLater(kj::EventLoop & loop, PyObject * func) { @@ -81,15 +79,7 @@ public: kj::Promise * promise = call_server_method(py_server, const_cast(methodName.cStr()), context); - PyObject * err = PyErr_Occurred(); - if(err) { - PyObject *ptype, *pvalue, *ptraceback; - PyErr_Fetch(&ptype, &pvalue, &ptraceback); - - char * errorMsg = PyString_AsString(pvalue); - PyErr_Clear(); - throw std::invalid_argument(errorMsg); - } + check_py_error(); if(promise == nullptr) return kj::READY_NOW; diff --git a/test/test_capability.py b/test/test_capability.py index 4352afc..857e319 100644 --- a/test/test_capability.py +++ b/test/test_capability.py @@ -113,7 +113,7 @@ def test_exception_client(capability): client = capability.TestInterface.new_client(BadServer(), loop) remote = client._send('foo', i=5) - with pytest.raises(RuntimeError): + with pytest.raises(ValueError): loop.wait_remote(remote) class BadPipelineServer: @@ -137,7 +137,7 @@ def test_exception_chain(capability): try: loop.wait_remote(remote) except Exception as e: - assert e.message == 'test' + assert str(e) == 'test' def test_pipeline_exception(capability): loop = capnp.EventLoop()