diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp
index 36fcc4d3611..0e91f9f3856 100644
--- a/src/qt/guiutil.cpp
+++ b/src/qt/guiutil.cpp
@@ -902,4 +902,15 @@ QString MakeHtmlLink(const QString& source, const QString& link)
QLatin1String("") % link % QLatin1String(""));
}
+void PrintSlotException(
+ const std::exception* exception,
+ const QObject* sender,
+ const QObject* receiver)
+{
+ std::string description = sender->metaObject()->className();
+ description += "->";
+ description += receiver->metaObject()->className();
+ PrintExceptionContinue(exception, description.c_str());
+}
+
} // namespace GUIUtil
diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h
index 6ab0a71a962..a1cf2743549 100644
--- a/src/qt/guiutil.h
+++ b/src/qt/guiutil.h
@@ -9,18 +9,23 @@
#include
#include
#include
+#include
+#include
#include
#include
#include
#include
#include
+#include
#include
#include
#include
#include
+#include
#include
+#include
class QValidatedLineEdit;
class SendCoinsRecipient;
@@ -332,6 +337,53 @@ namespace GUIUtil
*/
QString MakeHtmlLink(const QString& source, const QString& link);
+ void PrintSlotException(
+ const std::exception* exception,
+ const QObject* sender,
+ const QObject* receiver);
+
+ /**
+ * A drop-in replacement of QObject::connect function
+ * (see: https://doc.qt.io/qt-5/qobject.html#connect-3), that
+ * guaranties that all exceptions are handled within the slot.
+ *
+ * NOTE: This function is incompatible with Qt private signals.
+ */
+ template
+ auto ExceptionSafeConnect(
+ Sender sender, Signal signal, Receiver receiver, Slot method,
+ Qt::ConnectionType type = Qt::AutoConnection)
+ {
+ return QObject::connect(
+ sender, signal, receiver,
+ [sender, receiver, method](auto&&... args) {
+ bool ok{true};
+ try {
+ (receiver->*method)(std::forward(args)...);
+ } catch (const NonFatalCheckError& e) {
+ PrintSlotException(&e, sender, receiver);
+ ok = QMetaObject::invokeMethod(
+ qApp, "handleNonFatalException",
+ blockingGUIThreadConnection(),
+ Q_ARG(QString, QString::fromStdString(e.what())));
+ } catch (const std::exception& e) {
+ PrintSlotException(&e, sender, receiver);
+ ok = QMetaObject::invokeMethod(
+ qApp, "handleRunawayException",
+ blockingGUIThreadConnection(),
+ Q_ARG(QString, QString::fromStdString(e.what())));
+ } catch (...) {
+ PrintSlotException(nullptr, sender, receiver);
+ ok = QMetaObject::invokeMethod(
+ qApp, "handleRunawayException",
+ blockingGUIThreadConnection(),
+ Q_ARG(QString, "Unknown failure occurred."));
+ }
+ assert(ok);
+ },
+ type);
+ }
+
} // namespace GUIUtil
#endif // BITCOIN_QT_GUIUTIL_H