From dccd1cd348e04e8b85a8cd6b0cc2c1fe92accab4 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 30 Apr 2024 12:02:28 -0400 Subject: [PATCH] LibDesktop+TaskBar: Propagate errors from AppFile::spawn and friends Returning a boolean tells us nothing about why the spawn failed. --- Userland/Libraries/LibDesktop/AppFile.cpp | 28 ++++++++------------- Userland/Libraries/LibDesktop/AppFile.h | 4 +-- Userland/Services/Taskbar/TaskbarWindow.cpp | 4 +-- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibDesktop/AppFile.cpp b/Userland/Libraries/LibDesktop/AppFile.cpp index 0d743384bbf..99f44f65ce0 100644 --- a/Userland/Libraries/LibDesktop/AppFile.cpp +++ b/Userland/Libraries/LibDesktop/AppFile.cpp @@ -169,22 +169,19 @@ Vector AppFile::launcher_protocols() const return protocols; } -bool AppFile::spawn(ReadonlySpan arguments) const +ErrorOr AppFile::spawn(ReadonlySpan arguments) const { if (!is_valid()) - return false; + return Error::from_string_literal("AppFile is invalid"); - auto pid = Core::Process::spawn(executable(), arguments, working_directory()); - if (pid.is_error()) - return false; - - return true; + TRY(Core::Process::spawn(executable(), arguments, working_directory())); + return {}; } -bool AppFile::spawn_with_escalation(ReadonlySpan user_arguments) const +ErrorOr AppFile::spawn_with_escalation(ReadonlySpan user_arguments) const { if (!is_valid()) - return false; + return Error::from_string_literal("AppFile is invalid"); StringView exe; Vector args; @@ -207,18 +204,15 @@ bool AppFile::spawn_with_escalation(ReadonlySpan user_arguments) con } args.extend(Vector(user_arguments)); - auto pid = Core::Process::spawn(exe, args.span(), - working_directory().is_empty() ? Core::StandardPaths::home_directory() : working_directory()); - if (pid.is_error()) - return false; - - return true; + TRY(Core::Process::spawn(exe, args.span(), + working_directory().is_empty() ? Core::StandardPaths::home_directory() : working_directory())); + return {}; } void AppFile::spawn_with_escalation_or_show_error(GUI::Window& window, ReadonlySpan arguments) const { - if (!spawn_with_escalation(arguments)) - GUI::MessageBox::show_error(&window, ByteString::formatted("Failed to spawn {} with escalation", executable())); + if (auto result = spawn_with_escalation(arguments); result.is_error()) + GUI::MessageBox::show_error(&window, ByteString::formatted("Failed to spawn {} with escalation: {}", executable(), result.error())); } } diff --git a/Userland/Libraries/LibDesktop/AppFile.h b/Userland/Libraries/LibDesktop/AppFile.h index c78eaaa6cf7..10ed3265986 100644 --- a/Userland/Libraries/LibDesktop/AppFile.h +++ b/Userland/Libraries/LibDesktop/AppFile.h @@ -44,8 +44,8 @@ public: Vector launcher_mime_types() const; Vector launcher_file_types() const; Vector launcher_protocols() const; - bool spawn(ReadonlySpan arguments = {}) const; - bool spawn_with_escalation(ReadonlySpan arguments = {}) const; + ErrorOr spawn(ReadonlySpan arguments = {}) const; + ErrorOr spawn_with_escalation(ReadonlySpan arguments = {}) const; void spawn_with_escalation_or_show_error(GUI::Window&, ReadonlySpan arguments = {}) const; private: diff --git a/Userland/Services/Taskbar/TaskbarWindow.cpp b/Userland/Services/Taskbar/TaskbarWindow.cpp index cb106c681ec..1f96011fd2c 100644 --- a/Userland/Services/Taskbar/TaskbarWindow.cpp +++ b/Userland/Services/Taskbar/TaskbarWindow.cpp @@ -386,8 +386,8 @@ void TaskbarWindow::wm_event(GUI::WMEvent& event) break; } case GUI::Event::WM_SuperSpaceKeyPressed: { - if (!m_assistant_app_file->spawn()) - warnln("failed to spawn 'Assistant' when requested via Super+Space"); + if (auto result = m_assistant_app_file->spawn(); result.is_error()) + warnln("Failed to spawn 'Assistant' when requested via Super+Space: {}", result.error()); break; } case GUI::Event::WM_SuperDKeyPressed: {