6 #include <boost/python.hpp>
7 #include "knowrob/integration/python/PythonError.h"
8 #include "knowrob/Logger.h"
22 :
KnowRobError(errorData.exc_type, errorData.exc_msg, errorData.exc_trace),
40 if (!PyErr_Occurred()) {
42 KB_WARN(
"PythonError was created even though no error occurred in Python!");
47 PyObject * py_type, *py_value, *py_traceback;
48 PyErr_Fetch(&py_type, &py_value, &py_traceback);
50 KB_WARN(
"PythonError was created but PyErr_Fetch returned nullptr for error type.");
53 PyErr_NormalizeException(&py_type, &py_value, &py_traceback);
54 if (py_traceback !=
nullptr) {
55 PyException_SetTraceback(py_value, py_traceback);
59 handle<> h_type(py_type);
60 handle<> h_value(allow_null(py_value));
61 handle<> h_traceback(allow_null(py_traceback));
64 auto e_type = extract<std::string>(
object(h_type).attr(
"__name__"));
73 auto e_message = extract<std::string>(
object(h_value).attr(
"__str__")());
74 if (e_message.check()) {
75 errorData.
exc_msg = e_message();
81 object o_traceback(h_traceback);
83 auto tb_lineno = extract<long>(o_traceback.
85 if (tb_lineno.check()) {
89 auto e_file_path = extract<std::string>(o_traceback.
93 if (e_file_path.check()) {
98 object format_tb =
import(
"traceback").
101 object formatted_list = format_tb(o_traceback);
102 object formatted_traceback = str(
"\n").join(formatted_list).slice(0, -1);
103 auto str_traceback = extract<std::string>(formatted_traceback);
104 if (str_traceback.check()) {
void setFile(std::string_view file)
static ErrorData makeErrorData()
std::optional< std::string > exc_file
std::optional< int > exc_line