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
|
||||
* @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 {};
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user