Base::Exception extension

=========================

1. Enable automatic storing of information (function, file, line) when throwing the exception via macro:

Examples:

THROWM(Exception, "BSpline GeoId is out of bounds.")
THROWM(ValueError, "BSpline GeoId is out of bounds.")

THROW(AbortException)

Output:

a) Python Console (what()):
App.ActiveDocument.Sketch004.modifyBSplineKnotMultiplicity(16,3,0)
Traceback (most recent call last):
File "<input>", line 1, in <module>
Base.FreeCADError: FreeCAD exception thrown (BSpline GeoId is out of bounds.)

b) ReportException (report()):
Exception (Thu Apr 27 19:15:24 2017): BSpline GeoId is out of bounds. in bool Sketcher::SketchObject::modifyBSplineKnotMultiplicity(int, int, int) in src/Mod/Sketcher/App/SketchObject.cpp:4102

2. Extend the basic framework so as to allow more control over the mangling of the message introduced by the user, setting the basis to allow, where needed,
 to preserve the original message while allowing full legacy behaviour.

3. Supporting FileDialog reporting as legacy
This commit is contained in:
Abdullah Tahiri
2017-04-28 22:39:56 +02:00
committed by wmayer
parent c2eea67893
commit 5cbeb1002e
2 changed files with 116 additions and 9 deletions

View File

@@ -41,7 +41,7 @@ Exception::Exception(void)
}
Exception::Exception(const Exception &inst)
: BaseClass(),_sErrMsg(inst._sErrMsg)
: BaseClass(),_sErrMsg(inst._sErrMsg), _file(inst._file), _line(inst._line), _function(inst._function)
{
}
@@ -69,7 +69,30 @@ const char* Exception::what(void) const throw()
void Exception::ReportException (void) const
{
Console().Error("Exception (%s): %s \n",Console().Time(),what());
std::string str = "";
if(!_sErrMsg.empty())
str+= (_sErrMsg + " ");
if(!_function.empty()) {
str+="In ";
str+=_function;
str+= " ";
}
if(!_file.empty() && !_line.empty()) {
// strip absolute path
std::size_t pos = _file.find("src");
if (pos!=std::string::npos) {
str+="in ";
str+= _file.substr(pos);
str+= ":";
str+=_line;
}
}
Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str());
}
// ---------------------------------------------------------
@@ -144,33 +167,68 @@ FileException::FileException(const char * sMessage, const char * sFileName)
: Exception( sMessage ),file(sFileName)
{
if (sFileName) {
_sErrMsg += ": ";
_sErrMsg += sFileName;
_sErrMsgAndFileName = _sErrMsg + ": ";
_sErrMsgAndFileName += sFileName;
}
}
FileException::FileException(const char * sMessage, const FileInfo& File)
: Exception( sMessage ),file(File)
{
_sErrMsg += ": ";
_sErrMsg += File.fileName();
_sErrMsgAndFileName = _sErrMsg + ": ";
_sErrMsgAndFileName += File.fileName();
}
FileException::FileException()
: Exception( "Unknown file exeption happened" )
: Exception( "Unknown file exception happened" )
{
_sErrMsgAndFileName = _sErrMsg;
}
FileException::FileException(const FileException &inst)
: Exception( inst._sErrMsg.c_str() ),file(inst.file)
: Exception( inst._sErrMsg.c_str() ), file(inst.file), _sErrMsgAndFileName(inst._sErrMsgAndFileName.c_str())
{
}
std::string FileException::getFileName() const
{
return file.fileName();
}
const char* FileException::what() const throw()
{
return Exception::what();
return _sErrMsgAndFileName.c_str();
}
void FileException::ReportException (void) const
{
std::string str = "";
if(!_sErrMsgAndFileName.empty())
str+= (_sErrMsgAndFileName + " ");
if(!_function.empty()) {
str+="In ";
str+=_function;
str+= " ";
}
if(!_file.empty() && !_line.empty()) {
// strip absolute path
std::size_t pos = _file.find("src");
if (pos!=std::string::npos) {
str+="in ";
str+= _file.substr(pos);
str+= ":";
str+=_line;
}
}
Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str());
}
// ---------------------------------------------------------
FileSystemError::FileSystemError(const char * sMessage)
@@ -562,6 +620,7 @@ CADKernelError::CADKernelError(const CADKernelError &inst)
{
}
// ---------------------------------------------------------
#if defined(__GNUC__) && defined (FC_OS_LINUX)