Commit 849d1088 authored by Emmanuel Promayon's avatar Emmanuel Promayon

FIXED catch all possible exception inside the application, so that it won't crash

see https://doc.qt.io/qt-5/qcoreapplication.html#notify for more details.
This should send an error message instead of crashing imp!
For instance old bugzilla bug #169 does not crash the application anymore,
but just send an error message:
see https://bugzilla-timc.imag.fr/show_bug.cgi?id=169
parent 03800dab
......@@ -214,6 +214,32 @@ void Application::quitting() {
}
}
// ----------------- notify --------------------
bool Application::notify(QObject* receiver, QEvent* event) {
bool done = true;
std::exception_ptr otherException;
try {
done = QApplication::notify(receiver, event);
}
catch (const std::exception& e) {
CAMITK_ERROR(tr("Caught a std exception: %1").arg(e.what()))
}
catch (...) {
CAMITK_ERROR(tr("Caught an unknown exception"))
otherException = std::current_exception();
try {
if (otherException) {
std::rethrow_exception(otherException);
}
}
catch (const std::exception& e) {
CAMITK_ERROR(tr("Exception: %1").arg(e.what()))
}
}
return done;
}
// ----------------- setMainWindow --------------------
void Application::setMainWindow(MainWindow* mw) {
if (mw == NULL) {
......@@ -429,10 +455,10 @@ Component* Application::open(const QString& fileName) {
resetProgressBar();
CAMITK_ERROR_ALT(tr("ComponentExtension Opening Error: cannot find the appropriate component plugin for opening:\n\"%1\" (extension \"%2\" or \"%3\")\nTo solve this problem, make sure that:\n - A corresponding valid plugin is present in one of the following directories: \"%4\"\n - Your application loaded the the appropriate extension before trying to open a file")
.arg(fileName,
fileInfo.suffix(),
fileInfo.completeSuffix(),
Core::getComponentDirectories().join(", ")))
.arg(fileName,
fileInfo.suffix(),
fileInfo.completeSuffix(),
Core::getComponentDirectories().join(", ")))
}
else {
std::exception_ptr otherException;
......@@ -558,10 +584,10 @@ Component* Application::openDirectory(const QString& dirName, const QString& plu
restoreOverrideCursor();
resetProgressBar();
CAMITK_ERROR_ALT(tr("Opening Error: Cannot find the appropriate component plugin for opening directory:\n%1\n"
"To solve this problem, make sure that:\n"
" - A corresponding valid plugin is present in one of the component directories: \"%2\"\n"
" - And either your application is initialized with the autoloadExtensions option\n"
" - Or your correctly registered your component in the CamiTK settings").arg(dirName, Core::getComponentDirectories().join(", ")))
"To solve this problem, make sure that:\n"
" - A corresponding valid plugin is present in one of the component directories: \"%2\"\n"
" - And either your application is initialized with the autoloadExtensions option\n"
" - Or your correctly registered your component in the CamiTK settings").arg(dirName, Core::getComponentDirectories().join(", ")))
}
// restore the normal cursor/progress bar
......@@ -634,17 +660,17 @@ bool Application::save(Component* component) {
// -- check the validity of the plugin
if (!componentExtension) {
CAMITK_ERROR_ALT(tr("Saving Error: cannot find the appropriate component plugin for saving component:\n"
"\"%1\"\n"
"In file:\n"
"\"%1\" (extension \"%2\" or \"%3\")\n"
"To solve this problem, make sure that:\n"
" - A corresponding valid plugin is present in one of the component directories: \"%4\"\n"
" - And either your application is initialized with the autoloadExtensions option\n"
" - Or your correctly registered your component in the CamiTK settings")
.arg(component->getName(),
fileInfo.suffix(),
fileInfo.completeSuffix(),
Core::getComponentDirectories().join(", ")))
"\"%1\"\n"
"In file:\n"
"\"%1\" (extension \"%2\" or \"%3\")\n"
"To solve this problem, make sure that:\n"
" - A corresponding valid plugin is present in one of the component directories: \"%4\"\n"
" - And either your application is initialized with the autoloadExtensions option\n"
" - Or your correctly registered your component in the CamiTK settings")
.arg(component->getName(),
fileInfo.suffix(),
fileInfo.completeSuffix(),
Core::getComponentDirectories().join(", ")))
return false;
}
else {
......@@ -688,12 +714,12 @@ int Application::registerAllActions(ActionExtension* ext) {
// check if an action with same name was not already registered
if (getActionMap().contains(action->getName())) {
CAMITK_ERROR_ALT(tr("Cannot register action: %1 (extension: %2, family: %3, description: \"%4\")\n"
"extension of same name already registered by extension \"%5\"")
.arg(action->getName(),
action->getExtensionName(),
action->getFamily(),
action->getDescription(),
getAction(action->getName())->getExtensionName()))
"extension of same name already registered by extension \"%5\"")
.arg(action->getName(),
action->getExtensionName(),
action->getFamily(),
action->getDescription(),
getAction(action->getName())->getExtensionName()))
}
else {
getActionMap().insert(action->getName(), action);
......@@ -1127,7 +1153,7 @@ void Application::saveHistoryAsSXML() {
stateElement.appendChild(backTransitionElement);
// and "Next" appears on the right
stateElement.appendChild(nextTransitionElement);
} // End transitions
}
......
......@@ -103,6 +103,9 @@ public:
*/
/// @{
/// reimplemented from QApplication to catch all exception from external libs used in CEP (e.g. from ITK) and avoid crashes...
bool notify(QObject* , QEvent* );
/// get the application name
static QString getName();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment