From 78a2c6d9cfd979146e65c9b27bb5eb8b44528f3e Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 19 Apr 2018 23:51:56 +0200 Subject: [PATCH] Fix error handling in Ice/qt demo --- cpp11/Ice/qt/Client.cpp | 133 ++++++++++++++++++++++++++-------------- cpp11/Ice/qt/README.md | 4 +- cpp11/Ice/qt/qt.pro | 11 +++- 3 files changed, 98 insertions(+), 50 deletions(-) diff --git a/cpp11/Ice/qt/Client.cpp b/cpp11/Ice/qt/Client.cpp index 30caccc39..666ee957f 100644 --- a/cpp11/Ice/qt/Client.cpp +++ b/cpp11/Ice/qt/Client.cpp @@ -43,26 +43,6 @@ class DispatchEvent : public QEvent std::function _call; }; -QString -errorToString(const std::exception& err) -{ - return QString(typeid(err).name()).replace("class", "Error"); -} - -QString -errorToString(std::exception_ptr errptr) -{ - try - { - std::rethrow_exception(errptr); - } - catch (const std::exception& err) - { - return errorToString(err); - } - return ""; -} - } MainWindow::MainWindow() : @@ -115,26 +95,19 @@ MainWindow::MainWindow() : connect(_timeout, &QSlider::valueChanged, this, &MainWindow::updateProxy); connect(_mode, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::updateProxy); - try - { - Ice::InitializationData initData; - initData.properties = Ice::createProperties(); - initData.properties->load("config.client"); - initData.dispatcher = [this](std::function dispatchCall, const std::shared_ptr&) - { - QApplication::postEvent(this, new DispatchEvent(dispatchCall)); - }; - _communicator = Ice::initialize(initData); - updateProxy(); - } - catch(const std::exception& ex) - { - statusBar()->showMessage(errorToString(ex)); - } - auto centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget); + + Ice::InitializationData initData; + initData.properties = Ice::createProperties(); + initData.properties->load("config.client"); + initData.dispatcher = [this](std::function dispatchCall, const std::shared_ptr&) + { + QApplication::postEvent(this, new DispatchEvent(dispatchCall)); + }; + _communicator = Ice::initialize(initData); + updateProxy(); } bool @@ -147,9 +120,15 @@ MainWindow::event(QEvent* event) { dispatchEvent->dispatch(); } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } catch(const std::exception& ex) { - statusBar()->showMessage(errorToString(ex)); + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); } return true; } @@ -252,7 +231,20 @@ MainWindow::sayHello() }, [this](std::exception_ptr errptr) { - statusBar()->showMessage(errorToString(errptr)); + try + { + std::rethrow_exception(errptr); + } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } + catch(const std::exception& ex) + { + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); + } }, [this, mode](bool) { @@ -267,9 +259,15 @@ MainWindow::sayHello() }); } } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } catch(const std::exception& ex) { - statusBar()->showMessage(errorToString(ex)); + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); } } } @@ -277,7 +275,7 @@ MainWindow::sayHello() void MainWindow::shutdown() { - if (_helloPrx) + if(_helloPrx) { try { @@ -298,7 +296,20 @@ MainWindow::shutdown() }, [this](std::exception_ptr errptr) { - statusBar()->showMessage(errorToString(errptr)); + try + { + std::rethrow_exception(errptr); + } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } + catch(const std::exception& ex) + { + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); + } }, [this, mode](bool) { @@ -313,9 +324,15 @@ MainWindow::shutdown() }); } } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } catch(const std::exception& ex) { - statusBar()->showMessage(errorToString(ex)); + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); } } } @@ -327,7 +344,20 @@ MainWindow::flush() { _helloPrx->ice_flushBatchRequestsAsync([this](std::exception_ptr errptr) { - statusBar()->showMessage(errorToString(errptr)); + try + { + std::rethrow_exception(errptr); + } + catch(const Ice::Exception& ex) + { + statusBar()->showMessage(ex.ice_id().c_str()); + } + catch(const std::exception& ex) + { + QMessageBox error(QMessageBox::Critical, "Error", ex.what()); + error.exec(); + QApplication::exit(EXIT_FAILURE); + } }); _flush->setEnabled(false); statusBar()->showMessage("Flushed batch requests"); @@ -338,7 +368,16 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; - w.show(); - return a.exec(); + try + { + MainWindow w; + w.show(); + return a.exec(); + } + catch(const std::exception& ex) + { + QMessageBox error(QMessageBox::Critical, "Initialization exception", ex.what()); + error.exec(); + return EXIT_FAILURE; + } } diff --git a/cpp11/Ice/qt/README.md b/cpp11/Ice/qt/README.md index ce1fb53ba..07efe7ee3 100644 --- a/cpp11/Ice/qt/README.md +++ b/cpp11/Ice/qt/README.md @@ -11,8 +11,8 @@ The demo requires QT 5 package from https://www.qt.io/ ### Building the demo ``` -qmake -o qt.pro -Make +qmake +make ``` ## Building the demo on Windows diff --git a/cpp11/Ice/qt/qt.pro b/cpp11/Ice/qt/qt.pro index dd044958c..82ee67900 100644 --- a/cpp11/Ice/qt/qt.pro +++ b/cpp11/Ice/qt/qt.pro @@ -9,7 +9,8 @@ TEMPLATE = app TARGET = client DESTDIR = . -CONFIG += qt warn_on +CONFIG += qt warn_on debug +CONFIG -= app_bundle QT = core gui widgets @@ -23,6 +24,14 @@ DEFINES += ICE_CPP11_MAPPING INCLUDEPATH += . +macx { + ICE_HOME =? /usr/local +} +unix:!macx { + ICE_HOME =? /usr +} + + equals (ICE_HOME, "/usr") { } else:equals(ICE_HOME, "/usr/local") { } else:exists($(ICE_HOME)/include ) {