Skip to content

Commit

Permalink
Fix lost filename in err msg
Browse files Browse the repository at this point in the history
In some circumstances, FileExceptions are constructed empty, then have a
filename assigned to them, but the error message in these scenarios is
left as the default "unknown" one, which is sometimes shown to users.
This change fixes that case to be consistent with instances that are
constructed with the filename.
  • Loading branch information
heewa committed May 13, 2021
1 parent 91b0409 commit 8599b1e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/Base/Exception.cpp
Expand Up @@ -248,17 +248,13 @@ const char* XMLAttributeError::what() const throw()
FileException::FileException(const char * sMessage, const char * sFileName)
: Exception( sMessage ),file(sFileName)
{
if (sFileName) {
_sErrMsgAndFileName = _sErrMsg + ": ";
_sErrMsgAndFileName += sFileName;
}
setFileName(sFileName);
}

FileException::FileException(const char * sMessage, const FileInfo& File)
: Exception( sMessage ),file(File)
{
_sErrMsgAndFileName = _sErrMsg + ": ";
_sErrMsgAndFileName += File.fileName();
setFileName(File.fileName().c_str());
}

FileException::FileException()
Expand All @@ -274,6 +270,15 @@ FileException::FileException(const FileException &inst)
{
}

void FileException::setFileName(const char * sFileName) {
file.setFile(sFileName);
_sErrMsgAndFileName = _sErrMsg;
if (sFileName) {
_sErrMsgAndFileName += ": ";
_sErrMsgAndFileName += sFileName;
}
}

std::string FileException::getFileName() const
{
return file.fileName();
Expand Down Expand Up @@ -324,7 +329,7 @@ void FileException::setPyObject( PyObject * pydict)

Py::Dict edict(pydict);
if (edict.hasKey("filename"))
file.setFile(static_cast<std::string>(Py::String(edict.getItem("filename"))));
setFileName(Py::String(edict.getItem("filename")).as_std_string("utf-8").c_str());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Base/Exception.h
Expand Up @@ -262,6 +262,7 @@ class BaseExport FileException : public Exception
// necessary for what() legacy behaviour as it returns a buffer that
// can not be of a temporary object to be destroyed at end of what()
std::string _sErrMsgAndFileName;
void setFileName(const char * sFileName=0);
};

/**
Expand Down

0 comments on commit 8599b1e

Please sign in to comment.