From 5d5447aced2c6b2a16e7dc1efd6a235c478480fb Mon Sep 17 00:00:00 2001 From: Duke Date: Sat, 4 Mar 2023 12:02:32 -0500 Subject: [PATCH] Improve error handling when restoring from seedphrase We now catch exceptions in litelib_initialize_new_from_phrase and no longer save an empty/invalid wallet if there were errors. --- src/mainwindow.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6562979..4bd099d 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -209,16 +209,27 @@ MainWindow::MainWindow(QWidget *parent) : config->server = Settings::getInstance()->getSettings().server; // 3. Attempt to restore wallet with the seed phrase { - char* resp = litelib_initialize_new_from_phrase(config->dangerous, config->server.toStdString().c_str(), - seed.toStdString().c_str(), birthday, number); - QString reply = litelib_process_response(resp); - - if (reply.toUpper().trimmed() != "OK") { - QMessageBox::warning(this, tr("Failed to restore wallet"), + QString reply = ""; + try { + char* resp = litelib_initialize_new_from_phrase(config->dangerous, config->server.toStdString().c_str(), + seed.toStdString().c_str(), birthday, number); + reply = litelib_process_response(resp); + + if (reply.toUpper().trimmed() != "OK") { + QMessageBox::warning(this, tr("Failed to restore wallet"), + tr("Couldn't restore the wallet") + "\n" + reply, + QMessageBox::Ok); + return false; + } + } catch (const std::exception& e) { + //TODO: try another server with getRandomServer() + qDebug() << __func__ << ": caught an exception! Bailing out: " << e.what(); + QMessageBox::warning(this, tr("Failed to restore wallet"), tr("Couldn't restore the wallet") + "\n" + reply, QMessageBox::Ok); - - } + return false; + } + } // 4. Finally attempt to save the wallet