Skip to content

Commit

Permalink
Base: improve exception
Browse files Browse the repository at this point in the history
For better FC and Python exception mapping.
  • Loading branch information
realthunder authored and wwmayer committed Aug 17, 2019
1 parent aa7d780 commit 5941706
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 110 deletions.
8 changes: 8 additions & 0 deletions src/App/Application.cpp
Expand Up @@ -194,6 +194,7 @@ Base::ConsoleObserverFile *Application::_pConsoleObserverFile =0;

AppExport std::map<std::string,std::string> Application::mConfig;
BaseExport extern PyObject* Base::BaseExceptionFreeCADError;
BaseExport extern PyObject* Base::BaseExceptionFreeCADAbort;

//**************************************************************************
// Construction and destruction
Expand Down Expand Up @@ -314,6 +315,10 @@ Application::Application(std::map<std::string,std::string> &mConfig)
Py_INCREF(Base::BaseExceptionFreeCADError);
PyModule_AddObject(pBaseModule, "FreeCADError", Base::BaseExceptionFreeCADError);

Base::BaseExceptionFreeCADAbort = PyErr_NewException("Base.FreeCADAbort", PyExc_BaseException, NULL);
Py_INCREF(Base::BaseExceptionFreeCADAbort);
PyModule_AddObject(pBaseModule, "FreeCADAbort", Base::BaseExceptionFreeCADAbort);

// Python types
Base::Interpreter().addType(&Base::VectorPy ::Type,pBaseModule,"Vector");
Base::Interpreter().addType(&Base::MatrixPy ::Type,pBaseModule,"Matrix");
Expand Down Expand Up @@ -1319,6 +1324,7 @@ void Application::initTypes(void)
Base::Type ::init();
Base::BaseClass ::init();
Base::Exception ::init();
Base::AbortException ::init();
Base::Persistence ::init();

// Complex data classes
Expand Down Expand Up @@ -1461,6 +1467,8 @@ void Application::initTypes(void)
new ExceptionProducer<Base::TypeError>;
new ExceptionProducer<Base::ValueError>;
new ExceptionProducer<Base::IndexError>;
new ExceptionProducer<Base::NameError>;
new ExceptionProducer<Base::ImportError>;
new ExceptionProducer<Base::AttributeError>;
new ExceptionProducer<Base::RuntimeError>;
new ExceptionProducer<Base::BadGraphError>;
Expand Down
181 changes: 129 additions & 52 deletions src/Base/Exception.cpp
Expand Up @@ -31,6 +31,8 @@
#include "Console.h"
#include <CXX/Objects.hxx>

FC_LOG_LEVEL_INIT("Exception", true, true);

using namespace Base;


Expand Down Expand Up @@ -88,32 +90,17 @@ const char* Exception::what(void) const throw()
void Exception::ReportException (void) const
{
if (!_isReported) {
std::string str = "";

if (!_sErrMsg.empty())
str+= (_sErrMsg + " ");

if (!_function.empty()) {
str+="In ";
str+=_function;
str+= " ";
}

std::string _linestr = std::to_string(_line);

if (!_file.empty() && !_linestr.empty()) {
// strip absolute path
std::size_t pos = _file.find("src");

if (pos!=std::string::npos) {
str+="in ";
str+= _file.substr(pos);
str+= ":";
str+=_linestr;
}
}

Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str());
const char *msg;
if(_sErrMsg.empty())
msg = typeid(*this).name();
else
msg = _sErrMsg.c_str();
#ifdef FC_DEBUG
if(_function.size()) {
_FC_ERR(_file.c_str(),_line, _function << " -- " << msg);
}else
#endif
_FC_ERR(_file.c_str(),_line,msg);
_isReported = true;
}
}
Expand Down Expand Up @@ -164,6 +151,7 @@ void Exception::setPyObject( PyObject * pydict)

// ---------------------------------------------------------

TYPESYSTEM_SOURCE(Base::AbortException,Base::Exception);

AbortException::AbortException(const char * sMessage)
: Exception( sMessage )
Expand Down Expand Up @@ -315,32 +303,17 @@ const char* FileException::what() const throw()
void FileException::ReportException (void) const
{
if (!_isReported) {
std::string str = "";

if (!_sErrMsgAndFileName.empty())
str+= (_sErrMsgAndFileName + " ");

if (!_function.empty()) {
str+="In ";
str+=_function;
str+= " ";
}

std::string _linestr = std::to_string(_line);

if (!_file.empty() && !_linestr.empty()) {
// strip absolute path
std::size_t pos = _file.find("src");

if (pos!=std::string::npos) {
str+="in ";
str+= _file.substr(pos);
str+= ":";
str+=_linestr;
}
}

Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str());
const char *msg;
if(_sErrMsgAndFileName.empty())
msg = typeid(*this).name();
else
msg = _sErrMsgAndFileName.c_str();
#ifdef FC_DEBUG
if(_function.size()) {
_FC_ERR(_file.c_str(),_line, _function << " -- " << msg);
}else
#endif
_FC_ERR(_file.c_str(),_line,msg);
_isReported = true;
}
}
Expand All @@ -363,6 +336,10 @@ void FileException::setPyObject( PyObject * pydict)
}
}

PyObject * FileException::getPyExceptionType() const {
return PyExc_IOError;
}

// ---------------------------------------------------------


Expand Down Expand Up @@ -544,6 +521,10 @@ TypeError::TypeError(const TypeError &inst)
{
}

PyObject *TypeError::getPyExceptionType() const {
return PyExc_TypeError;
}

// ---------------------------------------------------------

ValueError::ValueError()
Expand All @@ -566,6 +547,10 @@ ValueError::ValueError(const ValueError &inst)
{
}

PyObject *ValueError::getPyExceptionType() const {
return PyExc_ValueError;
}

// ---------------------------------------------------------

IndexError::IndexError()
Expand All @@ -588,6 +573,62 @@ IndexError::IndexError(const IndexError &inst)
{
}

PyObject *IndexError::getPyExceptionType() const {
return PyExc_IndexError;
}

// ---------------------------------------------------------

NameError::NameError()
: Exception()
{
}

NameError::NameError(const char * sMessage)
: Exception(sMessage)
{
}

NameError::NameError(const std::string& sMessage)
: Exception(sMessage)
{
}

NameError::NameError(const NameError &inst)
: Exception(inst)
{
}

PyObject *NameError::getPyExceptionType() const {
return PyExc_NameError;
}

// ---------------------------------------------------------

ImportError::ImportError()
: Exception()
{
}

ImportError::ImportError(const char * sMessage)
: Exception(sMessage)
{
}

ImportError::ImportError(const std::string& sMessage)
: Exception(sMessage)
{
}

ImportError::ImportError(const ImportError &inst)
: Exception(inst)
{
}

PyObject *ImportError::getPyExceptionType() const {
return PyExc_ImportError;
}

// ---------------------------------------------------------

AttributeError::AttributeError()
Expand All @@ -610,6 +651,10 @@ AttributeError::AttributeError(const AttributeError &inst)
{
}

PyObject *AttributeError::getPyExceptionType() const {
return PyExc_AttributeError;
}

// ---------------------------------------------------------

RuntimeError::RuntimeError()
Expand All @@ -632,6 +677,10 @@ RuntimeError::RuntimeError(const RuntimeError &inst)
{
}

PyObject *RuntimeError::getPyExceptionType() const {
return PyExc_RuntimeError;
}

// ---------------------------------------------------------

BadGraphError::BadGraphError()
Expand Down Expand Up @@ -676,6 +725,10 @@ NotImplementedError::NotImplementedError(const NotImplementedError &inst)
{
}

PyObject *NotImplementedError::getPyExceptionType() const {
return PyExc_NotImplementedError;
}

// ---------------------------------------------------------

DivisionByZeroError::DivisionByZeroError()
Expand All @@ -698,6 +751,10 @@ DivisionByZeroError::DivisionByZeroError(const DivisionByZeroError &inst)
{
}

PyObject *DivisionByZeroError::getPyExceptionType() const {
return PyExc_ZeroDivisionError;
}

// ---------------------------------------------------------

ReferencesError::ReferencesError()
Expand All @@ -720,6 +777,10 @@ ReferencesError::ReferencesError(const ReferencesError &inst)
{
}

PyObject *ReferencesError::getPyExceptionType() const {
return PyExc_ReferenceError;
}

// ---------------------------------------------------------

ExpressionError::ExpressionError()
Expand Down Expand Up @@ -786,6 +847,10 @@ UnicodeError::UnicodeError(const UnicodeError &inst)
{
}

PyObject *UnicodeError::getPyExceptionType() const {
return PyExc_UnicodeError;
}

// ---------------------------------------------------------

OverflowError::OverflowError()
Expand All @@ -808,6 +873,10 @@ OverflowError::OverflowError(const OverflowError &inst)
{
}

PyObject *OverflowError::getPyExceptionType() const {
return PyExc_OverflowError;
}

// ---------------------------------------------------------

UnderflowError::UnderflowError()
Expand All @@ -830,6 +899,10 @@ UnderflowError::UnderflowError(const UnderflowError &inst)
{
}

PyObject *UnderflowError::getPyExceptionType() const {
return PyExc_ArithmeticError;
}

// ---------------------------------------------------------

UnitsMismatchError::UnitsMismatchError()
Expand All @@ -852,6 +925,10 @@ UnitsMismatchError::UnitsMismatchError(const UnitsMismatchError &inst)
{
}

PyObject *UnitsMismatchError::getPyExceptionType() const {
return PyExc_ArithmeticError;
}

// ---------------------------------------------------------

CADKernelError::CADKernelError()
Expand Down

0 comments on commit 5941706

Please sign in to comment.