|
9 | 9 | #include <fs.h>
|
10 | 10 | #include <net.h>
|
11 | 11 | #include <netaddress.h>
|
| 12 | +#include <util/check.h> |
12 | 13 |
|
| 14 | +#include <QApplication> |
13 | 15 | #include <QEvent>
|
14 | 16 | #include <QHeaderView>
|
15 | 17 | #include <QItemDelegate>
|
16 | 18 | #include <QLabel>
|
17 | 19 | #include <QMessageBox>
|
| 20 | +#include <QMetaObject> |
18 | 21 | #include <QObject>
|
19 | 22 | #include <QProgressBar>
|
20 | 23 | #include <QString>
|
21 | 24 | #include <QTableView>
|
22 | 25 |
|
| 26 | +#include <cassert> |
23 | 27 | #include <chrono>
|
| 28 | +#include <utility> |
24 | 29 |
|
25 | 30 | class QValidatedLineEdit;
|
26 | 31 | class SendCoinsRecipient;
|
@@ -332,6 +337,53 @@ namespace GUIUtil
|
332 | 337 | */
|
333 | 338 | QString MakeHtmlLink(const QString& source, const QString& link);
|
334 | 339 |
|
| 340 | + void PrintSlotException( |
| 341 | + const std::exception* exception, |
| 342 | + const QObject* sender, |
| 343 | + const QObject* receiver); |
| 344 | + |
| 345 | + /** |
| 346 | + * A drop-in replacement of QObject::connect function |
| 347 | + * (see: https://doc.qt.io/qt-5/qobject.html#connect-3), that |
| 348 | + * guaranties that all exceptions are handled within the slot. |
| 349 | + * |
| 350 | + * NOTE: This function is incompatible with Qt private signals. |
| 351 | + */ |
| 352 | + template <typename Sender, typename Signal, typename Receiver, typename Slot> |
| 353 | + auto ExceptionSafeConnect( |
| 354 | + Sender sender, Signal signal, Receiver receiver, Slot method, |
| 355 | + Qt::ConnectionType type = Qt::AutoConnection) |
| 356 | + { |
| 357 | + return QObject::connect( |
| 358 | + sender, signal, receiver, |
| 359 | + [sender, receiver, method](auto&&... args) { |
| 360 | + bool ok{true}; |
| 361 | + try { |
| 362 | + (receiver->*method)(std::forward<decltype(args)>(args)...); |
| 363 | + } catch (const NonFatalCheckError& e) { |
| 364 | + PrintSlotException(&e, sender, receiver); |
| 365 | + ok = QMetaObject::invokeMethod( |
| 366 | + qApp, "handleNonFatalException", |
| 367 | + blockingGUIThreadConnection(), |
| 368 | + Q_ARG(QString, QString::fromStdString(e.what()))); |
| 369 | + } catch (const std::exception& e) { |
| 370 | + PrintSlotException(&e, sender, receiver); |
| 371 | + ok = QMetaObject::invokeMethod( |
| 372 | + qApp, "handleRunawayException", |
| 373 | + blockingGUIThreadConnection(), |
| 374 | + Q_ARG(QString, QString::fromStdString(e.what()))); |
| 375 | + } catch (...) { |
| 376 | + PrintSlotException(nullptr, sender, receiver); |
| 377 | + ok = QMetaObject::invokeMethod( |
| 378 | + qApp, "handleRunawayException", |
| 379 | + blockingGUIThreadConnection(), |
| 380 | + Q_ARG(QString, "Unknown failure occurred.")); |
| 381 | + } |
| 382 | + assert(ok); |
| 383 | + }, |
| 384 | + type); |
| 385 | + } |
| 386 | + |
335 | 387 | } // namespace GUIUtil
|
336 | 388 |
|
337 | 389 | #endif // BITCOIN_QT_GUIUTIL_H
|
0 commit comments