Skip to content

Commit

Permalink
Make WebUI error non-fatal
Browse files Browse the repository at this point in the history
* Do not exit the app when WebUI is failed for start.
* Print the error to stdout.

PR #19697.
Closes #19695.
Closes #19469.
  • Loading branch information
HanabishiRecca authored Oct 24, 2023
1 parent 6860c0d commit 35e4b35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,13 +890,17 @@ int Application::exec()
#ifndef DISABLE_WEBUI
m_webui = new WebUI(this);
#ifdef DISABLE_GUI
if (m_webui->isErrored())
QCoreApplication::exit(EXIT_FAILURE);
connect(m_webui, &WebUI::fatalError, this, []() { QCoreApplication::exit(EXIT_FAILURE); });
connect(m_webui, &WebUI::error, this, [](const QString &message) { fprintf(stderr, "%s\n", qUtf8Printable(message)); });

printf("%s", qUtf8Printable(u"\n******** %1 ********\n"_s.arg(tr("Information"))));

if (m_webui->isEnabled())
if (m_webui->isErrored())
{
const QString error = m_webui->errorMessage() + u'\n'
+ tr("To fix the error, you may need to edit the config file manually.");
fprintf(stderr, "%s\n", qUtf8Printable(error));
}
else if (m_webui->isEnabled())
{
const QHostAddress address = m_webui->hostAddress();
const QString url = u"%1://%2:%3"_s.arg((m_webui->isHttps() ? u"https"_s : u"http"_s)
Expand Down
14 changes: 10 additions & 4 deletions src/webui/webui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ WebUI::WebUI(IApplication *app)
void WebUI::configure()
{
m_isErrored = false; // clear previous error state
m_errorMsg.clear();

const QString portForwardingProfile = u"webui"_s;
const Preferences *pref = Preferences::instance();
Expand Down Expand Up @@ -113,13 +114,13 @@ void WebUI::configure()
}
else
{
const QString errorMsg = tr("Web UI: Unable to bind to IP: %1, port: %2. Reason: %3")
m_errorMsg = tr("Web UI: Unable to bind to IP: %1, port: %2. Reason: %3")
.arg(serverAddressString).arg(port).arg(m_httpServer->errorString());
LogMsg(errorMsg, Log::CRITICAL);
qCritical() << errorMsg;
LogMsg(m_errorMsg, Log::CRITICAL);
qCritical() << m_errorMsg;

m_isErrored = true;
emit fatalError();
emit error(m_errorMsg);
}
}

Expand Down Expand Up @@ -156,6 +157,11 @@ bool WebUI::isErrored() const
return m_isErrored;
}

QString WebUI::errorMessage() const
{
return m_errorMsg;
}

bool WebUI::isHttps() const
{
if (!m_httpServer) return false;
Expand Down
4 changes: 3 additions & 1 deletion src/webui/webui.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,21 @@ class WebUI final : public ApplicationComponent<QObject>

bool isEnabled() const;
bool isErrored() const;
QString errorMessage() const;
bool isHttps() const;
QHostAddress hostAddress() const;
quint16 port() const;

signals:
void fatalError();
void error(const QString &message);

private slots:
void configure();

private:
bool m_isEnabled = false;
bool m_isErrored = false;
QString m_errorMsg;
QPointer<Http::Server> m_httpServer;
QPointer<Net::DNSUpdater> m_dnsUpdater;
QPointer<WebApplication> m_webapp;
Expand Down

0 comments on commit 35e4b35

Please sign in to comment.