|
9 | 9 | #include <fs.h>
|
10 | 10 | #include <qt/guiconstants.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 <QMessageBox>
|
| 19 | +#include <QMetaObject> |
17 | 20 | #include <QObject>
|
18 | 21 | #include <QProgressBar>
|
19 | 22 | #include <QString>
|
20 | 23 | #include <QTableView>
|
21 | 24 | #include <QLabel>
|
22 | 25 |
|
| 26 | +#include <cassert> |
23 | 27 | #include <chrono>
|
| 28 | +#include <utility> |
24 | 29 |
|
25 | 30 | class QValidatedLineEdit;
|
26 | 31 | class OptionsModel;
|
@@ -520,6 +525,58 @@ namespace GUIUtil
|
520 | 525 | QObject::connect(&source, &QObject::destroyed, object, std::forward<Fn>(function), connection);
|
521 | 526 | }
|
522 | 527 |
|
| 528 | + /** |
| 529 | + * Replaces a plain text link with an HTML tagged one. |
| 530 | + */ |
| 531 | + QString MakeHtmlLink(const QString& source, const QString& link); |
| 532 | + |
| 533 | + void PrintSlotException( |
| 534 | + const std::exception* exception, |
| 535 | + const QObject* sender, |
| 536 | + const QObject* receiver); |
| 537 | + |
| 538 | + /** |
| 539 | + * A drop-in replacement of QObject::connect function |
| 540 | + * (see: https://doc.qt.io/qt-5/qobject.html#connect-3), that |
| 541 | + * guaranties that all exceptions are handled within the slot. |
| 542 | + * |
| 543 | + * NOTE: This function is incompatible with Qt private signals. |
| 544 | + */ |
| 545 | + template <typename Sender, typename Signal, typename Receiver, typename Slot> |
| 546 | + auto ExceptionSafeConnect( |
| 547 | + Sender sender, Signal signal, Receiver receiver, Slot method, |
| 548 | + Qt::ConnectionType type = Qt::AutoConnection) |
| 549 | + { |
| 550 | + return QObject::connect( |
| 551 | + sender, signal, receiver, |
| 552 | + [sender, receiver, method](auto&&... args) { |
| 553 | + bool ok{true}; |
| 554 | + try { |
| 555 | + (receiver->*method)(std::forward<decltype(args)>(args)...); |
| 556 | + } catch (const NonFatalCheckError& e) { |
| 557 | + PrintSlotException(&e, sender, receiver); |
| 558 | + ok = QMetaObject::invokeMethod( |
| 559 | + qApp, "handleNonFatalException", |
| 560 | + blockingGUIThreadConnection(), |
| 561 | + Q_ARG(QString, QString::fromStdString(e.what()))); |
| 562 | + } catch (const std::exception& e) { |
| 563 | + PrintSlotException(&e, sender, receiver); |
| 564 | + ok = QMetaObject::invokeMethod( |
| 565 | + qApp, "handleRunawayException", |
| 566 | + blockingGUIThreadConnection(), |
| 567 | + Q_ARG(QString, QString::fromStdString(e.what()))); |
| 568 | + } catch (...) { |
| 569 | + PrintSlotException(nullptr, sender, receiver); |
| 570 | + ok = QMetaObject::invokeMethod( |
| 571 | + qApp, "handleRunawayException", |
| 572 | + blockingGUIThreadConnection(), |
| 573 | + Q_ARG(QString, "Unknown failure occurred.")); |
| 574 | + } |
| 575 | + assert(ok); |
| 576 | + }, |
| 577 | + type); |
| 578 | + } |
| 579 | + |
523 | 580 | } // namespace GUIUtil
|
524 | 581 |
|
525 | 582 | #endif // BITCOIN_QT_GUIUTIL_H
|
0 commit comments