From a8494980da660f679646b48bfa14bcf7bc8f31fc Mon Sep 17 00:00:00 2001 From: sudden6 Date: Wed, 27 Jun 2018 09:22:24 +0200 Subject: [PATCH] refactor(core): fix error handling --- src/core/core.cpp | 44 +++++++++++++++++++++++++++++-------- src/core/core.h | 12 +++++++--- src/nexus.cpp | 4 ++-- src/persistence/profile.cpp | 14 +++++++++++- src/persistence/profile.h | 4 ++++ 5 files changed, 63 insertions(+), 15 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 4c30b0bfb..e59a659c6 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -100,7 +100,8 @@ void Core::registerCallbacks(Tox * tox) { * @param settings Settings specific to Core * @return nullptr or a Core object ready to start */ -ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * const settings) +ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * const settings, + ToxCoreErrors *err) { QThread* thread = new QThread(); if (thread == nullptr) { @@ -112,11 +113,17 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c auto toxOptions = ToxOptions::makeToxOptions(savedata, settings); if (toxOptions == nullptr) { qCritical() << "could not allocate Tox Options data structure"; + if(err) { + *err = ToxCoreErrors::ERROR_ALLOC; + } return {}; } ToxCorePtr core(new Core(thread)); if(core == nullptr) { + if(err) { + *err = ToxCoreErrors::ERROR_ALLOC; + } return {}; } @@ -129,6 +136,9 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c case TOX_ERR_NEW_LOAD_BAD_FORMAT: qCritical() << "failed to parse Tox save data"; + if(err) { + *err = ToxCoreErrors::BAD_PROXY; + } return {}; case TOX_ERR_NEW_PORT_ALLOC: @@ -143,38 +153,53 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c } qCritical() << "can't to bind the port"; + if(err) { + *err = ToxCoreErrors::FAILED_TO_START; + } return {}; case TOX_ERR_NEW_PROXY_BAD_HOST: case TOX_ERR_NEW_PROXY_BAD_PORT: case TOX_ERR_NEW_PROXY_BAD_TYPE: qCritical() << "bad proxy, error code:" << tox_err; - //emit badProxy(); + if(err) { + *err = ToxCoreErrors::BAD_PROXY; + } return {}; case TOX_ERR_NEW_PROXY_NOT_FOUND: qCritical() << "proxy not found"; - //emit badProxy(); + if(err) { + *err = ToxCoreErrors::BAD_PROXY; + } return {}; case TOX_ERR_NEW_LOAD_ENCRYPTED: qCritical() << "attempted to load encrypted Tox save data"; - //emit failedToStart(); + if(err) { + *err = ToxCoreErrors::INVALID_SAVE; + } return {}; case TOX_ERR_NEW_MALLOC: qCritical() << "memory allocation failed"; - //emit failedToStart(); + if(err) { + *err = ToxCoreErrors::ERROR_ALLOC; + } return {}; case TOX_ERR_NEW_NULL: qCritical() << "a parameter was null"; - //emit failedToStart(); + if(err) { + *err = ToxCoreErrors::FAILED_TO_START; + } return {}; default: qCritical() << "Tox core failed to start, unknown error code:" << tox_err; - //emit failedToStart(); + if(err) { + *err = ToxCoreErrors::FAILED_TO_START; + } return {}; } @@ -186,8 +211,9 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c core->av = new CoreAV(core->tox); if (!core->av || !core->av->getToxAv()) { qCritical() << "Toxav failed to start"; - //emit failedToStart(); - //deadifyTox(); + if(err) { + *err = ToxCoreErrors::FAILED_TO_START; + } return {}; } diff --git a/src/core/core.h b/src/core/core.h index 930be4c78..eced60d3e 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -56,7 +56,15 @@ class Core : public QObject Q_OBJECT public: - static ToxCorePtr makeToxCore(const QByteArray& savedata, const ICoreSettings* const settings); + enum class ToxCoreErrors { + BAD_PROXY, + INVALID_SAVE, + FAILED_TO_START, + ERROR_ALLOC + }; + + static ToxCorePtr makeToxCore(const QByteArray& savedata, const ICoreSettings* const settings, + ToxCoreErrors* err = nullptr); static Core* getInstance(); const CoreAV* getAv() const; CoreAV* getAv(); @@ -173,8 +181,6 @@ signals: void failedToSetStatus(Status status); void failedToSetTyping(bool typing); - void failedToStart(); - void badProxy(); void avReady(); void fileSendStarted(ToxFile file); diff --git a/src/nexus.cpp b/src/nexus.cpp index 413d383aa..bf8b31558 100644 --- a/src/nexus.cpp +++ b/src/nexus.cpp @@ -194,9 +194,9 @@ void Nexus::showMainGUI() connect(core, &Core::connected, widget, &Widget::onConnected); connect(core, &Core::disconnected, widget, &Widget::onDisconnected); - connect(core, &Core::failedToStart, widget, &Widget::onFailedToStartCore, + connect(profile, &Profile::failedToStart, widget, &Widget::onFailedToStartCore, Qt::BlockingQueuedConnection); - connect(core, &Core::badProxy, widget, &Widget::onBadProxyCore, Qt::BlockingQueuedConnection); + connect(profile, &Profile::badProxy, widget, &Widget::onBadProxyCore, Qt::BlockingQueuedConnection); connect(core, &Core::statusSet, widget, &Widget::onStatusSet); connect(core, &Core::usernameSet, widget, &Widget::setUsername); connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage); diff --git a/src/persistence/profile.cpp b/src/persistence/profile.cpp index 9b0f2f417..143a6fa1b 100644 --- a/src/persistence/profile.cpp +++ b/src/persistence/profile.cpp @@ -60,8 +60,20 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const s.setCurrentProfile(name); s.saveGlobal(); - core = Core::makeToxCore(toxsave, &s); + Core::ToxCoreErrors err; + core = Core::makeToxCore(toxsave, &s, &err); if(!core) { + switch (err) { + case Core::ToxCoreErrors::BAD_PROXY: + emit badProxy(); + break; + case Core::ToxCoreErrors::ERROR_ALLOC: + case Core::ToxCoreErrors::FAILED_TO_START: + case Core::ToxCoreErrors::INVALID_SAVE: + default: + emit failedToStart(); + } + qDebug() << "failed to start ToxCore"; return; } diff --git a/src/persistence/profile.h b/src/persistence/profile.h index 8a72b4ceb..ee11ecf8d 100644 --- a/src/persistence/profile.h +++ b/src/persistence/profile.h @@ -82,6 +82,10 @@ public: signals: void selfAvatarChanged(const QPixmap& pixmap); + // TODO(sudden6): this doesn't seem to be the right place for Core errors + void failedToStart(); + void badProxy(); + public slots: void onRequestSent(const ToxPk& friendPk, const QString& message);