Skip to content

Commit

Permalink
App: improve exception handling in FeaturePython
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder authored and wwmayer committed Sep 27, 2019
1 parent 0f29155 commit b450f5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/App/Document.cpp
Expand Up @@ -3418,6 +3418,7 @@ int Document::_recomputeFeature(DocumentObject* Feat)
}
catch(Base::AbortException &e){
e.ReportException();
FC_ERR("Failed to recompute " << Feat->getFullName() << ": " << e.what());
d->addRecomputeLog("User abort",Feat);
return -1;
}
Expand All @@ -3428,6 +3429,7 @@ int Document::_recomputeFeature(DocumentObject* Feat)
}
catch (Base::Exception &e) {
e.ReportException();
FC_ERR("Failed to recompute " << Feat->getFullName() << ": " << e.what());
d->addRecomputeLog(e.what(),Feat);
return 1;
}
Expand All @@ -3449,11 +3451,7 @@ int Document::_recomputeFeature(DocumentObject* Feat)
}else{
returnCode->Which = Feat;
d->addRecomputeLog(returnCode);
#ifdef FC_DEBUG
FC_ERR("Failed to recompute " << Feat->getFullName() << ": " << returnCode->Why);
#else
FC_LOG("Failed to recompute " << Feat->getFullName() << ": " << returnCode->Why);
#endif
return 1;
}
return 0;
Expand Down
20 changes: 8 additions & 12 deletions src/App/FeaturePython.cpp
Expand Up @@ -88,11 +88,7 @@ bool FeaturePythonImp::execute()
PyErr_Clear();
return false;
}
Base::PyException e; // extract the Python error text
e.ReportException();
std::stringstream str;
str << object->Label.getValue() << ": " << e.what();
throw Base::RuntimeError(str.str());
Base::PyException::ThrowException(); // extract the Python error text
}

return false;
Expand Down Expand Up @@ -165,7 +161,7 @@ bool FeaturePythonImp::onBeforeChangeLabel(std::string &newLabel)
Py::Object ret(Base::pyCall(py_onBeforeChangeLabel.ptr(),args.ptr()));
if(!ret.isNone()) {
if(!ret.isString())
throw Base::TypeError("onBeforeChangeLabel expects to return a string");
throw Py::TypeError("onBeforeChangeLabel expects to return a string");
newLabel = ret.as_string();
return true;
}
Expand Down Expand Up @@ -252,14 +248,14 @@ bool FeaturePythonImp::getSubObject(DocumentObject *&ret, const char *subname,
if(!res.isTrue())
return false;
if(!res.isSequence())
throw Base::TypeError("getSubObject expects return type of tuple");
throw Py::TypeError("getSubObject expects return type of tuple");
Py::Sequence seq(res);
if(seq.length() < 2 ||
(!seq.getItem(0).isNone() &&
!PyObject_TypeCheck(seq.getItem(0).ptr(),&DocumentObjectPy::Type)) ||
!PyObject_TypeCheck(seq.getItem(1).ptr(),&Base::MatrixPy::Type))
{
throw Base::TypeError("getSubObject expects return type of (obj,matrix,pyobj)");
throw Py::TypeError("getSubObject expects return type of (obj,matrix,pyobj)");
}
if(_mat)
*_mat = *static_cast<Base::MatrixPy*>(seq.getItem(1).ptr())->getMatrixPtr();
Expand Down Expand Up @@ -298,12 +294,12 @@ bool FeaturePythonImp::getSubObjects(std::vector<std::string> &ret, int reason)
if(!res.isTrue())
return true;
if(!res.isSequence())
throw Base::TypeError("getSubObjects expects return type of tuple");
throw Py::TypeError("getSubObjects expects return type of tuple");
Py::Sequence seq(res);
for(size_t i=0;i<seq.length();++i) {
Py::Object name(seq[i].ptr());
if(!name.isString())
throw Base::TypeError("getSubObjects expects string in returned sequence");
throw Py::TypeError("getSubObjects expects string in returned sequence");
ret.push_back(name.as_string());
}
return true;
Expand Down Expand Up @@ -340,14 +336,14 @@ bool FeaturePythonImp::getLinkedObject(DocumentObject *&ret, bool recurse,
return true;
}
if(!res.isSequence())
throw Base::TypeError("getLinkedObject expects return type of (object,matrix)");
throw Py::TypeError("getLinkedObject expects return type of (object,matrix)");
Py::Sequence seq(res);
if(seq.length() != 2 ||
(!seq.getItem(0).isNone() &&
!PyObject_TypeCheck(seq.getItem(0).ptr(),&DocumentObjectPy::Type)) ||
!PyObject_TypeCheck(seq.getItem(1).ptr(),&Base::MatrixPy::Type))
{
throw Base::TypeError("getLinkedObject expects return type of (object,matrix)");
throw Py::TypeError("getLinkedObject expects return type of (object,matrix)");
}
if(_mat)
*_mat = *static_cast<Base::MatrixPy*>(seq.getItem(1).ptr())->getMatrixPtr();
Expand Down

0 comments on commit b450f5a

Please sign in to comment.