mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(core): fix error handling
This commit is contained in:
parent
8574162949
commit
a8494980da
|
@ -100,7 +100,8 @@ void Core::registerCallbacks(Tox * tox) {
|
||||||
* @param settings Settings specific to Core
|
* @param settings Settings specific to Core
|
||||||
* @return nullptr or a Core object ready to start
|
* @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();
|
QThread* thread = new QThread();
|
||||||
if (thread == nullptr) {
|
if (thread == nullptr) {
|
||||||
|
@ -112,11 +113,17 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c
|
||||||
auto toxOptions = ToxOptions::makeToxOptions(savedata, settings);
|
auto toxOptions = ToxOptions::makeToxOptions(savedata, settings);
|
||||||
if (toxOptions == nullptr) {
|
if (toxOptions == nullptr) {
|
||||||
qCritical() << "could not allocate Tox Options data structure";
|
qCritical() << "could not allocate Tox Options data structure";
|
||||||
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::ERROR_ALLOC;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ToxCorePtr core(new Core(thread));
|
ToxCorePtr core(new Core(thread));
|
||||||
if(core == nullptr) {
|
if(core == nullptr) {
|
||||||
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::ERROR_ALLOC;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,6 +136,9 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c
|
||||||
|
|
||||||
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
|
case TOX_ERR_NEW_LOAD_BAD_FORMAT:
|
||||||
qCritical() << "failed to parse Tox save data";
|
qCritical() << "failed to parse Tox save data";
|
||||||
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::BAD_PROXY;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_PORT_ALLOC:
|
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";
|
qCritical() << "can't to bind the port";
|
||||||
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::FAILED_TO_START;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_PROXY_BAD_HOST:
|
case TOX_ERR_NEW_PROXY_BAD_HOST:
|
||||||
case TOX_ERR_NEW_PROXY_BAD_PORT:
|
case TOX_ERR_NEW_PROXY_BAD_PORT:
|
||||||
case TOX_ERR_NEW_PROXY_BAD_TYPE:
|
case TOX_ERR_NEW_PROXY_BAD_TYPE:
|
||||||
qCritical() << "bad proxy, error code:" << tox_err;
|
qCritical() << "bad proxy, error code:" << tox_err;
|
||||||
//emit badProxy();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::BAD_PROXY;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_PROXY_NOT_FOUND:
|
case TOX_ERR_NEW_PROXY_NOT_FOUND:
|
||||||
qCritical() << "proxy not found";
|
qCritical() << "proxy not found";
|
||||||
//emit badProxy();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::BAD_PROXY;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_LOAD_ENCRYPTED:
|
case TOX_ERR_NEW_LOAD_ENCRYPTED:
|
||||||
qCritical() << "attempted to load encrypted Tox save data";
|
qCritical() << "attempted to load encrypted Tox save data";
|
||||||
//emit failedToStart();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::INVALID_SAVE;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_MALLOC:
|
case TOX_ERR_NEW_MALLOC:
|
||||||
qCritical() << "memory allocation failed";
|
qCritical() << "memory allocation failed";
|
||||||
//emit failedToStart();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::ERROR_ALLOC;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
case TOX_ERR_NEW_NULL:
|
case TOX_ERR_NEW_NULL:
|
||||||
qCritical() << "a parameter was null";
|
qCritical() << "a parameter was null";
|
||||||
//emit failedToStart();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::FAILED_TO_START;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
default:
|
default:
|
||||||
qCritical() << "Tox core failed to start, unknown error code:" << tox_err;
|
qCritical() << "Tox core failed to start, unknown error code:" << tox_err;
|
||||||
//emit failedToStart();
|
if(err) {
|
||||||
|
*err = ToxCoreErrors::FAILED_TO_START;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,8 +211,9 @@ ToxCorePtr Core::makeToxCore(const QByteArray &savedata, const ICoreSettings * c
|
||||||
core->av = new CoreAV(core->tox);
|
core->av = new CoreAV(core->tox);
|
||||||
if (!core->av || !core->av->getToxAv()) {
|
if (!core->av || !core->av->getToxAv()) {
|
||||||
qCritical() << "Toxav failed to start";
|
qCritical() << "Toxav failed to start";
|
||||||
//emit failedToStart();
|
if(err) {
|
||||||
//deadifyTox();
|
*err = ToxCoreErrors::FAILED_TO_START;
|
||||||
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,15 @@ class Core : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
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();
|
static Core* getInstance();
|
||||||
const CoreAV* getAv() const;
|
const CoreAV* getAv() const;
|
||||||
CoreAV* getAv();
|
CoreAV* getAv();
|
||||||
|
@ -173,8 +181,6 @@ signals:
|
||||||
void failedToSetStatus(Status status);
|
void failedToSetStatus(Status status);
|
||||||
void failedToSetTyping(bool typing);
|
void failedToSetTyping(bool typing);
|
||||||
|
|
||||||
void failedToStart();
|
|
||||||
void badProxy();
|
|
||||||
void avReady();
|
void avReady();
|
||||||
|
|
||||||
void fileSendStarted(ToxFile file);
|
void fileSendStarted(ToxFile file);
|
||||||
|
|
|
@ -194,9 +194,9 @@ void Nexus::showMainGUI()
|
||||||
|
|
||||||
connect(core, &Core::connected, widget, &Widget::onConnected);
|
connect(core, &Core::connected, widget, &Widget::onConnected);
|
||||||
connect(core, &Core::disconnected, widget, &Widget::onDisconnected);
|
connect(core, &Core::disconnected, widget, &Widget::onDisconnected);
|
||||||
connect(core, &Core::failedToStart, widget, &Widget::onFailedToStartCore,
|
connect(profile, &Profile::failedToStart, widget, &Widget::onFailedToStartCore,
|
||||||
Qt::BlockingQueuedConnection);
|
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::statusSet, widget, &Widget::onStatusSet);
|
||||||
connect(core, &Core::usernameSet, widget, &Widget::setUsername);
|
connect(core, &Core::usernameSet, widget, &Widget::setUsername);
|
||||||
connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage);
|
connect(core, &Core::statusMessageSet, widget, &Widget::setStatusMessage);
|
||||||
|
|
|
@ -60,8 +60,20 @@ Profile::Profile(QString name, const QString& password, bool isNewProfile, const
|
||||||
s.setCurrentProfile(name);
|
s.setCurrentProfile(name);
|
||||||
s.saveGlobal();
|
s.saveGlobal();
|
||||||
|
|
||||||
core = Core::makeToxCore(toxsave, &s);
|
Core::ToxCoreErrors err;
|
||||||
|
core = Core::makeToxCore(toxsave, &s, &err);
|
||||||
if(!core) {
|
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";
|
qDebug() << "failed to start ToxCore";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,10 @@ public:
|
||||||
signals:
|
signals:
|
||||||
void selfAvatarChanged(const QPixmap& pixmap);
|
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:
|
public slots:
|
||||||
void onRequestSent(const ToxPk& friendPk, const QString& message);
|
void onRequestSent(const ToxPk& friendPk, const QString& message);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user