Merge pull request #9940 from glassez/error-msg

Show error message when Session failed to start
This commit is contained in:
Vladimir Golovnev 2018-12-09 11:16:17 +03:00 committed by GitHub
commit fc08091765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 36 deletions

View File

@ -44,6 +44,7 @@
#endif #endif
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include <QMessageBox>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <QSessionManager> #include <QSessionManager>
#include <QSharedMemory> #include <QSharedMemory>
@ -61,6 +62,7 @@
#include "base/bittorrent/session.h" #include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h" #include "base/bittorrent/torrenthandle.h"
#include "base/exceptions.h"
#include "base/iconprovider.h" #include "base/iconprovider.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/net/downloadmanager.h" #include "base/net/downloadmanager.h"
@ -495,26 +497,42 @@ int Application::exec(const QStringList &params)
GuiIconProvider::initInstance(); GuiIconProvider::initInstance();
#endif #endif
BitTorrent::Session::initInstance(); try {
connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentFinished, this, &Application::torrentFinished); BitTorrent::Session::initInstance();
connect(BitTorrent::Session::instance(), &BitTorrent::Session::allTorrentsFinished, this, &Application::allTorrentsFinished, Qt::QueuedConnection); connect(BitTorrent::Session::instance(), &BitTorrent::Session::torrentFinished, this, &Application::torrentFinished);
connect(BitTorrent::Session::instance(), &BitTorrent::Session::allTorrentsFinished, this, &Application::allTorrentsFinished, Qt::QueuedConnection);
#ifndef DISABLE_COUNTRIES_RESOLUTION #ifndef DISABLE_COUNTRIES_RESOLUTION
Net::GeoIPManager::initInstance(); Net::GeoIPManager::initInstance();
#endif #endif
ScanFoldersModel::initInstance(this); ScanFoldersModel::initInstance(this);
#ifndef DISABLE_WEBUI #ifndef DISABLE_WEBUI
m_webui = new WebUI; m_webui = new WebUI;
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
if (m_webui->isErrored()) if (m_webui->isErrored())
return 1; return 1;
connect(m_webui, &WebUI::fatalError, this, []() { QCoreApplication::exit(1); }); connect(m_webui, &WebUI::fatalError, this, []() { QCoreApplication::exit(1); });
#endif // DISABLE_GUI #endif // DISABLE_GUI
#endif // DISABLE_WEBUI #endif // DISABLE_WEBUI
new RSS::Session; // create RSS::Session singleton new RSS::Session; // create RSS::Session singleton
new RSS::AutoDownloader; // create RSS::AutoDownloader singleton new RSS::AutoDownloader; // create RSS::AutoDownloader singleton
}
catch (const RuntimeError &err) {
#ifdef DISABLE_GUI
fprintf(stderr, "%s", err.what());
#else
QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(tr("Application failed to start."));
msgBox.setInformativeText(err.message());
msgBox.show(); // Need to be shown or to moveToCenter does not work
msgBox.move(Utils::Misc::screenCenter(&msgBox));
msgBox.exec();
#endif
return 1;
}
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
#ifndef DISABLE_WEBUI #ifndef DISABLE_WEBUI

View File

@ -38,12 +38,12 @@ AsyncFileStorage::AsyncFileStorage(const QString &storageFolderPath, QObject *pa
, m_lockFile(m_storageDir.absoluteFilePath(QStringLiteral("storage.lock"))) , m_lockFile(m_storageDir.absoluteFilePath(QStringLiteral("storage.lock")))
{ {
if (!m_storageDir.mkpath(m_storageDir.absolutePath())) if (!m_storageDir.mkpath(m_storageDir.absolutePath()))
throw AsyncFileStorageError( throw AsyncFileStorageError {tr("Could not create directory '%1'.")
QString("Could not create directory '%1'.").arg(m_storageDir.absolutePath())); .arg(m_storageDir.absolutePath())};
// TODO: This folder locking approach does not work for UNIX systems. Implement it. // TODO: This folder locking approach does not work for UNIX systems. Implement it.
if (!m_lockFile.open(QFile::WriteOnly)) if (!m_lockFile.open(QFile::WriteOnly))
throw AsyncFileStorageError(m_lockFile.errorString()); throw AsyncFileStorageError {m_lockFile.errorString()};
} }
AsyncFileStorage::~AsyncFileStorage() AsyncFileStorage::~AsyncFileStorage()
@ -76,13 +76,3 @@ void AsyncFileStorage::store_impl(const QString &fileName, const QByteArray &dat
} }
} }
} }
AsyncFileStorageError::AsyncFileStorageError(const QString &message)
: std::runtime_error(message.toUtf8().data())
{
}
QString AsyncFileStorageError::message() const
{
return what();
}

View File

@ -28,22 +28,22 @@
#pragma once #pragma once
#include <stdexcept>
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QObject> #include <QObject>
class AsyncFileStorageError : public std::runtime_error #include "base/exceptions.h"
class AsyncFileStorageError : public RuntimeError
{ {
public: public:
explicit AsyncFileStorageError(const QString &message); using RuntimeError::RuntimeError;
QString message() const;
}; };
class AsyncFileStorage : public QObject class AsyncFileStorage : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(AsyncFileStorage)
public: public:
explicit AsyncFileStorage(const QString &storageFolderPath, QObject *parent = nullptr); explicit AsyncFileStorage(const QString &storageFolderPath, QObject *parent = nullptr);

View File

@ -69,6 +69,7 @@
#endif #endif
#include "base/algorithm.h" #include "base/algorithm.h"
#include "base/exceptions.h"
#include "base/global.h" #include "base/global.h"
#include "base/logger.h" #include "base/logger.h"
#include "base/net/downloadhandler.h" #include "base/net/downloadhandler.h"
@ -3817,11 +3818,11 @@ void Session::initResumeFolder()
if (resumeFolderDir.exists() || resumeFolderDir.mkpath(resumeFolderDir.absolutePath())) { if (resumeFolderDir.exists() || resumeFolderDir.mkpath(resumeFolderDir.absolutePath())) {
m_resumeFolderLock.setFileName(resumeFolderDir.absoluteFilePath("session.lock")); m_resumeFolderLock.setFileName(resumeFolderDir.absoluteFilePath("session.lock"));
if (!m_resumeFolderLock.open(QFile::WriteOnly)) { if (!m_resumeFolderLock.open(QFile::WriteOnly)) {
throw std::runtime_error("Cannot write to torrent resume folder."); throw RuntimeError {tr("Cannot write to torrent resume folder.")};
} }
} }
else { else {
throw std::runtime_error("Cannot create torrent resume folder."); throw RuntimeError {tr("Cannot create torrent resume folder.")};
} }
} }

View File

@ -30,11 +30,10 @@
RuntimeError::RuntimeError(const QString &message) RuntimeError::RuntimeError(const QString &message)
: std::runtime_error {message.toUtf8().data()} : std::runtime_error {message.toUtf8().data()}
, m_message {message}
{ {
} }
QString RuntimeError::message() const QString RuntimeError::message() const
{ {
return m_message; return what();
} }

View File

@ -36,7 +36,4 @@ class RuntimeError : public std::runtime_error
public: public:
explicit RuntimeError(const QString &message = ""); explicit RuntimeError(const QString &message = "");
QString message() const; QString message() const;
private:
const QString m_message;
}; };