Have a central place for catch-based logging of linux native notifications

This commit is contained in:
Ilya Fedin 2022-07-26 05:45:22 +04:00 committed by John Preston
parent d2e3c01c11
commit ff2bd86744

View file

@ -49,6 +49,18 @@ bool ServiceRegistered = false;
std::optional<ServerInformation> CurrentServerInformation; std::optional<ServerInformation> CurrentServerInformation;
QStringList CurrentCapabilities; QStringList CurrentCapabilities;
void Noexcept(Fn<void()> callback) noexcept {
try {
callback();
} catch (const Glib::Error &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
}
std::unique_ptr<base::Platform::DBus::ServiceWatcher> CreateServiceWatcher() { std::unique_ptr<base::Platform::DBus::ServiceWatcher> CreateServiceWatcher() {
try { try {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
@ -98,24 +110,22 @@ void StartServiceAsync(Fn<void()> callback) {
connection, connection,
std::string(kService), std::string(kService),
[=](Fn<DBus::StartReply()> result) { [=](Fn<DBus::StartReply()> result) {
try { Noexcept([&] {
result(); // get the error if any try {
} catch (const Glib::Error &e) { result(); // get the error if any
static const auto NotSupportedErrors = { } catch (const Glib::Error &e) {
"org.freedesktop.DBus.Error.ServiceUnknown", static const auto NotSupportedErrors = {
}; "org.freedesktop.DBus.Error.ServiceUnknown",
};
const auto errorName = const auto errorName =
Gio::DBus::ErrorUtils::get_remote_error(e); Gio::DBus::ErrorUtils::get_remote_error(e);
if (!ranges::contains(NotSupportedErrors, errorName)) { if (!ranges::contains(NotSupportedErrors, errorName)) {
LOG(("Native Notification Error: %1").arg( throw e;
QString::fromStdString(e.what()))); }
} }
} catch (const std::exception &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main(callback); crl::on_main(callback);
}); });
@ -161,7 +171,7 @@ bool GetServiceRegistered() {
void GetServerInformation( void GetServerInformation(
Fn<void(const std::optional<ServerInformation> &)> callback) { Fn<void(const std::optional<ServerInformation> &)> callback) {
try { Noexcept([&] {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -171,7 +181,7 @@ void GetServerInformation(
"GetServerInformation", "GetServerInformation",
{}, {},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try { Noexcept([&] {
auto reply = connection->call_finish(result); auto reply = connection->call_finish(result);
const auto name = GlibVariantCast<Glib::ustring>( const auto name = GlibVariantCast<Glib::ustring>(
@ -198,29 +208,20 @@ void GetServerInformation(
}); });
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback(std::nullopt); }); crl::on_main([=] { callback(std::nullopt); });
}, },
std::string(kService)); std::string(kService));
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback(std::nullopt); }); crl::on_main([=] { callback(std::nullopt); });
} }
void GetCapabilities(Fn<void(const QStringList &)> callback) { void GetCapabilities(Fn<void(const QStringList &)> callback) {
try { Noexcept([&] {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -230,7 +231,7 @@ void GetCapabilities(Fn<void(const QStringList &)> callback) {
"GetCapabilities", "GetCapabilities",
{}, {},
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try { Noexcept([&] {
auto reply = connection->call_finish(result); auto reply = connection->call_finish(result);
QStringList value; QStringList value;
@ -245,23 +246,14 @@ void GetCapabilities(Fn<void(const QStringList &)> callback) {
}); });
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback({}); }); crl::on_main([=] { callback({}); });
}, },
std::string(kService)); std::string(kService));
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback({}); }); crl::on_main([=] { callback({}); });
} }
@ -273,7 +265,7 @@ void GetInhibited(Fn<void(bool)> callback) {
return; return;
} }
try { Noexcept([&] {
const auto connection = Gio::DBus::Connection::get_sync( const auto connection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
@ -286,7 +278,7 @@ void GetInhibited(Fn<void(bool)> callback) {
Glib::ustring("Inhibited"), Glib::ustring("Inhibited"),
}), }),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try { Noexcept([&] {
auto reply = connection->call_finish(result); auto reply = connection->call_finish(result);
const auto value = GlibVariantCast<bool>( const auto value = GlibVariantCast<bool>(
@ -298,23 +290,14 @@ void GetInhibited(Fn<void(bool)> callback) {
}); });
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback(false); }); crl::on_main([=] { callback(false); });
}, },
std::string(kService)); std::string(kService));
return; return;
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main([=] { callback(false); }); crl::on_main([=] { callback(false); });
} }
@ -425,12 +408,12 @@ bool NotificationData::init(
const QString &subtitle, const QString &subtitle,
const QString &msg, const QString &msg,
Window::Notifications::Manager::DisplayOptions options) { Window::Notifications::Manager::DisplayOptions options) {
try { Noexcept([&] {
_dbusConnection = Gio::DBus::Connection::get_sync( _dbusConnection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what()))); if (!_dbusConnection) {
return false; return false;
} }
@ -444,7 +427,7 @@ bool NotificationData::init(
const Glib::ustring &interface_name, const Glib::ustring &interface_name,
const Glib::ustring &signal_name, const Glib::ustring &signal_name,
Glib::VariantContainerBase parameters) { Glib::VariantContainerBase parameters) {
try { Noexcept([&] {
if (signal_name == "ActionInvoked") { if (signal_name == "ActionInvoked") {
const auto id = GlibVariantCast<uint>( const auto id = GlibVariantCast<uint>(
parameters.get_child(0)); parameters.get_child(0));
@ -478,10 +461,7 @@ bool NotificationData::init(
crl::on_main(weak, [=] { notificationClosed(id, reason); }); crl::on_main(weak, [=] { notificationClosed(id, reason); });
} }
} catch (const std::exception &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
}; };
_title = title.toStdString(); _title = title.toStdString();
@ -622,24 +602,19 @@ void NotificationData::show() {
-1, -1,
}), }),
[=](const Glib::RefPtr<Gio::AsyncResult> &result) { [=](const Glib::RefPtr<Gio::AsyncResult> &result) {
try { Noexcept([&] {
auto reply = connection->call_finish(result); auto reply = connection->call_finish(result);
const auto notificationId = GlibVariantCast<uint>( const auto notificationId = GlibVariantCast<uint>(
reply.get_child(0)); reply.get_child(0));
crl::on_main(weak, [=] { crl::on_main(weak, [=] {
_notificationId = notificationId; _notificationId = notificationId;
}); });
return;
} catch (const Glib::Error &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
} catch (const std::exception &e) {
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
crl::on_main(weak, [=] {
_manager->clearNotification(_id);
}); });
if (!_notificationId) {
crl::on_main(weak, [=] {
_manager->clearNotification(_id);
});
}
}, },
std::string(kService)); std::string(kService));
})); }));
@ -893,12 +868,12 @@ Manager::Private::Private(not_null<Manager*> manager, Type type)
.arg(capabilities.join(", "))); .arg(capabilities.join(", ")));
} }
try { Noexcept([&] {
_dbusConnection = Gio::DBus::Connection::get_sync( _dbusConnection = Gio::DBus::Connection::get_sync(
Gio::DBus::BusType::BUS_TYPE_SESSION); Gio::DBus::BusType::BUS_TYPE_SESSION);
} catch (const Glib::Error &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what()))); if (!_dbusConnection) {
return; return;
} }
@ -914,7 +889,7 @@ Manager::Private::Private(not_null<Manager*> manager, Type type)
const Glib::ustring &interface_name, const Glib::ustring &interface_name,
const Glib::ustring &signal_name, const Glib::ustring &signal_name,
Glib::VariantContainerBase parameters) { Glib::VariantContainerBase parameters) {
try { Noexcept([&] {
const auto interface = GlibVariantCast<Glib::ustring>( const auto interface = GlibVariantCast<Glib::ustring>(
parameters.get_child(0)); parameters.get_child(0));
@ -926,10 +901,7 @@ Manager::Private::Private(not_null<Manager*> manager, Type type)
GlibVariantCast< GlibVariantCast<
std::map<Glib::ustring, Glib::VariantBase> std::map<Glib::ustring, Glib::VariantBase>
>(parameters.get_child(1)).at("Inhibited")); >(parameters.get_child(1)).at("Inhibited"));
} catch (const std::exception &e) { });
LOG(("Native Notification Error: %1").arg(
QString::fromStdString(e.what())));
}
}, },
std::string(kService), std::string(kService),
std::string(kPropertiesInterface), std::string(kPropertiesInterface),