Skip to content

Commit

Permalink
Fix error handling in Ice/qt demo
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone committed Apr 19, 2018
1 parent ed979fd commit 78a2c6d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 50 deletions.
133 changes: 86 additions & 47 deletions cpp11/Ice/qt/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,6 @@ class DispatchEvent : public QEvent
std::function<void()> _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() :
Expand Down Expand Up @@ -115,26 +95,19 @@ MainWindow::MainWindow() :
connect(_timeout, &QSlider::valueChanged, this, &MainWindow::updateProxy);
connect(_mode, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::updateProxy);

try
{
Ice::InitializationData initData;
initData.properties = Ice::createProperties();
initData.properties->load("config.client");
initData.dispatcher = [this](std::function<void()> dispatchCall, const std::shared_ptr<Ice::Connection>&)
{
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<void()> dispatchCall, const std::shared_ptr<Ice::Connection>&)
{
QApplication::postEvent(this, new DispatchEvent(dispatchCall));
};
_communicator = Ice::initialize(initData);
updateProxy();
}

bool
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -267,17 +259,23 @@ 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);
}
}
}

void
MainWindow::shutdown()
{
if (_helloPrx)
if(_helloPrx)
{
try
{
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
Expand All @@ -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");
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions cpp11/Ice/qt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion cpp11/Ice/qt/qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 ) {
Expand Down

0 comments on commit 78a2c6d

Please # to comment.