Skip to content

Commit 5907a5c

Browse files
committed
gui: Add ExceptionSafeConnect that takes a lambda
There is a variant of QObject::connect which takes 3 arguments. Add a variant of ExceptioNSafeConnect that does the same thing.
1 parent a2aab6d commit 5907a5c

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/qt/guiutil.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,11 @@ void PrintSlotException(
970970
{
971971
std::string description = sender->metaObject()->className();
972972
description += "->";
973-
description += receiver->metaObject()->className();
973+
if (receiver) {
974+
description += receiver->metaObject()->className();
975+
} else {
976+
description += "anonymous function";
977+
}
974978
PrintExceptionContinue(exception, description);
975979
}
976980

src/qt/guiutil.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ namespace GUIUtil
399399
},
400400
type);
401401
}
402+
template <typename Sender, typename Signal, typename Slot>
403+
auto ExceptionSafeConnect(
404+
Sender sender, Signal signal, Slot method)
405+
{
406+
return QObject::connect(
407+
sender, signal,
408+
[sender, method](auto&&... args) {
409+
bool ok{true};
410+
try {
411+
method(std::forward<decltype(args)>(args)...);
412+
} catch (const NonFatalCheckError& e) {
413+
PrintSlotException(&e, sender, nullptr);
414+
ok = QMetaObject::invokeMethod(
415+
qApp, "handleNonFatalException",
416+
blockingGUIThreadConnection(),
417+
Q_ARG(QString, QString::fromStdString(e.what())));
418+
} catch (const std::exception& e) {
419+
PrintSlotException(&e, sender, nullptr);
420+
ok = QMetaObject::invokeMethod(
421+
qApp, "handleRunawayException",
422+
blockingGUIThreadConnection(),
423+
Q_ARG(QString, QString::fromStdString(e.what())));
424+
} catch (...) {
425+
PrintSlotException(nullptr, sender, nullptr);
426+
ok = QMetaObject::invokeMethod(
427+
qApp, "handleRunawayException",
428+
blockingGUIThreadConnection(),
429+
Q_ARG(QString, "Unknown failure occurred."));
430+
}
431+
assert(ok);
432+
});
433+
}
402434

403435
/**
404436
* Shows a QDialog instance asynchronously, and deletes it on close.

0 commit comments

Comments
 (0)