18
18
#define _SRC_COMMON_CPP_API_MAPBASE_INL_
21
#include "Interface/Squeak.hh"
21
22
#include "src/common_cpp/API/APIExceptions.hh"
22
23
#include "src/common_cpp/Utils/PyObjectWrapper.hh"
23
24
#include "src/common_cpp/Utils/Exception.hh"
40
41
template<typename TYPE>
41
42
PyObject* MapBase<TYPE>::process_pyobj(PyObject* py_input) const {
42
43
// this function owns cpp_data; py_input is still owned by caller
43
TYPE* cpp_data = PyObjectWrapper::unwrap<TYPE>(py_input);
44
TYPE* cpp_data = NULL;
46
cpp_data = PyObjectWrapper::unwrap<TYPE>(py_input);
45
47
_process(cpp_data);
47
catch (Exception& s) {
48
CppErrorHandler::getInstance()->HandleExceptionNoJson(s, _classname);
49
} catch (std::exception& e) {
50
CppErrorHandler::getInstance()->HandleStdExcNoJson(e, _classname);
48
} catch (MAUS::Exception& exc) {
49
Squeak::mout(Squeak::debug) << "Stack trace:" << exc.GetStackTrace()
51
HandleException(&cpp_data, &exc, _classname);
52
} catch (std::exception& exc) {
53
HandleException(&cpp_data, &exc, _classname);
52
55
throw Exception(Exception::recoverable,
53
56
_classname+" threw an unhandled exception",
54
57
"MapBase::process_pyobj");
57
59
PyObject* py_output = PyObjectWrapper::wrap(cpp_data);
58
60
// py_output now owns cpp_data
64
// Need some wrapper code for the exception handler as the interface is
65
// assumed to be json. As conversion is error prone, need to be a bit careful
66
// and do the conversion here.
67
template <typename TYPE>
68
void MapBase<TYPE>::HandleException(TYPE** data,
70
std::string class_name) const {
74
Json::Value* val = NULL;
77
val = ConverterFactory().convert<TYPE, Json::Value>(*data);
79
// do nothing; catch data == NULL or failed conversion to json
81
delete *data; // we don't need it any more
82
if (val == NULL) { // conversion failed, try to build from scratch
84
data_temp.SetSpill(new Spill());
85
val = ConverterFactory().convert<MAUS::Data, Json::Value>(&data_temp);
87
*val = CppErrorHandler::getInstance()->HandleStdExc(*val, *exc, class_name);
88
*data = ConverterFactory().convert<Json::Value, TYPE>(val);
91
std::cerr << "Unhandled exception" << std::endl;
64
96
}// end of namespace