Base: improve exception

For better FC and Python exception mapping.
This commit is contained in:
Zheng, Lei
2019-06-21 10:46:43 +08:00
committed by wmayer
parent ea8965cbc2
commit 8f16147a40
9 changed files with 311 additions and 110 deletions

View File

@@ -31,6 +31,8 @@
#include "Console.h"
#include <CXX/Objects.hxx>
FC_LOG_LEVEL_INIT("Exception", true, true);
using namespace Base;
@@ -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;
}
}
@@ -164,6 +151,7 @@ void Exception::setPyObject( PyObject * pydict)
// ---------------------------------------------------------
TYPESYSTEM_SOURCE(Base::AbortException,Base::Exception);
AbortException::AbortException(const char * sMessage)
: Exception( sMessage )
@@ -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;
}
}
@@ -363,6 +336,10 @@ void FileException::setPyObject( PyObject * pydict)
}
}
PyObject * FileException::getPyExceptionType() const {
return PyExc_IOError;
}
// ---------------------------------------------------------
@@ -544,6 +521,10 @@ TypeError::TypeError(const TypeError &inst)
{
}
PyObject *TypeError::getPyExceptionType() const {
return PyExc_TypeError;
}
// ---------------------------------------------------------
ValueError::ValueError()
@@ -566,6 +547,10 @@ ValueError::ValueError(const ValueError &inst)
{
}
PyObject *ValueError::getPyExceptionType() const {
return PyExc_ValueError;
}
// ---------------------------------------------------------
IndexError::IndexError()
@@ -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()
@@ -610,6 +651,10 @@ AttributeError::AttributeError(const AttributeError &inst)
{
}
PyObject *AttributeError::getPyExceptionType() const {
return PyExc_AttributeError;
}
// ---------------------------------------------------------
RuntimeError::RuntimeError()
@@ -632,6 +677,10 @@ RuntimeError::RuntimeError(const RuntimeError &inst)
{
}
PyObject *RuntimeError::getPyExceptionType() const {
return PyExc_RuntimeError;
}
// ---------------------------------------------------------
BadGraphError::BadGraphError()
@@ -676,6 +725,10 @@ NotImplementedError::NotImplementedError(const NotImplementedError &inst)
{
}
PyObject *NotImplementedError::getPyExceptionType() const {
return PyExc_NotImplementedError;
}
// ---------------------------------------------------------
DivisionByZeroError::DivisionByZeroError()
@@ -698,6 +751,10 @@ DivisionByZeroError::DivisionByZeroError(const DivisionByZeroError &inst)
{
}
PyObject *DivisionByZeroError::getPyExceptionType() const {
return PyExc_ZeroDivisionError;
}
// ---------------------------------------------------------
ReferencesError::ReferencesError()
@@ -720,6 +777,10 @@ ReferencesError::ReferencesError(const ReferencesError &inst)
{
}
PyObject *ReferencesError::getPyExceptionType() const {
return PyExc_ReferenceError;
}
// ---------------------------------------------------------
ExpressionError::ExpressionError()
@@ -786,6 +847,10 @@ UnicodeError::UnicodeError(const UnicodeError &inst)
{
}
PyObject *UnicodeError::getPyExceptionType() const {
return PyExc_UnicodeError;
}
// ---------------------------------------------------------
OverflowError::OverflowError()
@@ -808,6 +873,10 @@ OverflowError::OverflowError(const OverflowError &inst)
{
}
PyObject *OverflowError::getPyExceptionType() const {
return PyExc_OverflowError;
}
// ---------------------------------------------------------
UnderflowError::UnderflowError()
@@ -830,6 +899,10 @@ UnderflowError::UnderflowError(const UnderflowError &inst)
{
}
PyObject *UnderflowError::getPyExceptionType() const {
return PyExc_ArithmeticError;
}
// ---------------------------------------------------------
UnitsMismatchError::UnitsMismatchError()
@@ -852,6 +925,10 @@ UnitsMismatchError::UnitsMismatchError(const UnitsMismatchError &inst)
{
}
PyObject *UnitsMismatchError::getPyExceptionType() const {
return PyExc_ArithmeticError;
}
// ---------------------------------------------------------
CADKernelError::CADKernelError()