Fix exception propogation in Promises
This commit is contained in:
@@ -11,6 +11,7 @@ extern "C" {
|
||||
::kj::Promise<void> * call_server_method(PyObject * py_server, char * name, capnp::CallContext< capnp::DynamicStruct, capnp::DynamicStruct> & context);
|
||||
PyObject * wrap_kj_exception(kj::Exception &);
|
||||
PyObject * wrap_kj_exception_for_reraise(kj::Exception &);
|
||||
PyObject * get_exception_info(PyObject *, PyObject *, PyObject *);
|
||||
}
|
||||
|
||||
void reraise_kj_exception() {
|
||||
@@ -36,8 +37,22 @@ void reraise_kj_exception() {
|
||||
void check_py_error() {
|
||||
PyObject * err = PyErr_Occurred();
|
||||
if(err) {
|
||||
// PyErr_Clear();
|
||||
throw std::exception();
|
||||
// TODO: decref references
|
||||
PyObject * ptype, *pvalue, *ptraceback;
|
||||
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
|
||||
|
||||
PyObject * info = get_exception_info(ptype, pvalue, ptraceback);
|
||||
|
||||
PyObject * py_filename = PyTuple_GetItem(info, 0);
|
||||
kj::String filename(kj::heapString(PyBytes_AsString(py_filename)));
|
||||
|
||||
PyObject * py_line = PyTuple_GetItem(info, 1);
|
||||
int line = PyInt_AsLong(py_line);
|
||||
|
||||
PyObject * py_description = PyTuple_GetItem(info, 2);
|
||||
kj::String description(kj::heapString(PyBytes_AsString(py_description)));
|
||||
|
||||
throw kj::Exception(kj::Exception::Nature::OTHER, kj::Exception::Durability::PERMANENT, kj::mv(filename), line, kj::mv(description));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ from cython.operator cimport dereference as deref
|
||||
cimport async_cpp
|
||||
|
||||
from cpython.ref cimport PyObject, Py_INCREF, Py_DECREF
|
||||
from cpython.exc cimport PyErr_Clear
|
||||
from libc.stdint cimport *
|
||||
ctypedef unsigned int uint
|
||||
ctypedef uint8_t UInt8
|
||||
@@ -181,6 +182,7 @@ class KjException(Exception):
|
||||
return self.message
|
||||
|
||||
cdef public object wrap_kj_exception(capnp.Exception & exception):
|
||||
PyErr_Clear()
|
||||
wrapper = _KjExceptionWrapper()._init(exception)
|
||||
ret = KjException(wrapper=wrapper)
|
||||
|
||||
@@ -204,6 +206,13 @@ cdef public object wrap_kj_exception_for_reraise(capnp.Exception & exception):
|
||||
ret = KjException(wrapper=wrapper)
|
||||
return ret
|
||||
|
||||
cdef public object get_exception_info(object exc_type, object exc_obj, object exc_tb):
|
||||
try:
|
||||
return (exc_tb.tb_frame.f_code.co_filename, exc_tb.tb_lineno, repr(exc_type) + ':' + str(exc_obj))
|
||||
except:
|
||||
return ('', 0, "Couldn't determine python exception")
|
||||
|
||||
|
||||
ctypedef fused _DynamicStructReaderOrBuilder:
|
||||
_DynamicStructReader
|
||||
_DynamicStructBuilder
|
||||
|
||||
Reference in New Issue
Block a user