From 26eee2c02dcb2621f2756eaf3a5116aa5e06fefa Mon Sep 17 00:00:00 2001 From: creator1creeper1 Date: Fri, 7 Jan 2022 14:50:07 +0100 Subject: [PATCH] SystemMonitor: Propagate errors using try_set_main_widget build_process_window now uses try_set_main_widget and might return an error. process_properties_action handles a possible error by simply not updating the process window if an error occured while building it. --- Userland/Applications/SystemMonitor/main.cpp | 33 +++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Userland/Applications/SystemMonitor/main.cpp b/Userland/Applications/SystemMonitor/main.cpp index cf1b61b604..7f13b5413c 100644 --- a/Userland/Applications/SystemMonitor/main.cpp +++ b/Userland/Applications/SystemMonitor/main.cpp @@ -51,7 +51,7 @@ #include #include -static NonnullRefPtr build_process_window(pid_t); +static ErrorOr> build_process_window(pid_t); static NonnullRefPtr build_storage_widget(); static NonnullRefPtr build_hardware_tab(); static NonnullRefPtr build_performance_tab(); @@ -139,20 +139,20 @@ ErrorOr serenity_main(Main::Arguments arguments) window->set_title("System Monitor"); window->resize(560, 430); - auto& main_widget = window->set_main_widget(); - main_widget.set_layout(); - main_widget.set_fill_with_background_color(true); + auto main_widget = TRY(window->try_set_main_widget()); + main_widget->set_layout(); + main_widget->set_fill_with_background_color(true); // Add a tasteful separating line between the menu and the main UI. - auto& top_line = main_widget.add(Gfx::Orientation::Horizontal); + auto& top_line = main_widget->add(Gfx::Orientation::Horizontal); top_line.set_fixed_height(2); - auto& tabwidget_container = main_widget.add(); + auto& tabwidget_container = main_widget->add(); tabwidget_container.set_layout(); tabwidget_container.layout()->set_margins({ 0, 4, 4 }); auto& tabwidget = tabwidget_container.add(); - statusbar = main_widget.add(3); + statusbar = main_widget->add(3); auto process_model = ProcessModel::create(); process_model->on_state_update = [&](int process_count, int thread_count) { @@ -280,7 +280,10 @@ ErrorOr serenity_main(Main::Arguments arguments) RefPtr process_window; auto it = process_windows.find(pid); if (it == process_windows.end()) { - process_window = build_process_window(pid); + auto process_window_or_error = build_process_window(pid); + if (process_window_or_error.is_error()) + return; + process_window = process_window_or_error.release_value(); process_window->on_close_request = [pid, &process_windows] { process_windows.remove(pid); return GUI::Window::CloseRequestDecision::Close; @@ -396,17 +399,17 @@ public: } }; -NonnullRefPtr build_process_window(pid_t pid) +ErrorOr> build_process_window(pid_t pid) { auto window = GUI::Window::construct(); window->resize(480, 360); window->set_title(String::formatted("PID {} - System Monitor", pid)); - auto& main_widget = window->set_main_widget(); - main_widget.set_fill_with_background_color(true); - main_widget.set_layout(); + auto main_widget = TRY(window->try_set_main_widget()); + main_widget->set_fill_with_background_color(true); + main_widget->set_layout(); - auto& hero_container = main_widget.add(); + auto& hero_container = main_widget->add(); hero_container.set_shrink_to_fit(true); hero_container.set_layout(); hero_container.layout()->set_margins(4); @@ -436,10 +439,10 @@ NonnullRefPtr build_process_window(pid_t pid) process_index.sibling_at_column(ProcessModel::Column::Name).data().to_string(), pid)); - auto& separator = main_widget.add(); + auto& separator = main_widget->add(); separator.set_fixed_height(2); - auto& widget_stack = main_widget.add(); + auto& widget_stack = main_widget->add(); auto& unavailable_process_widget = widget_stack.add(String::formatted("Unable to access PID {}", pid)); auto& process_tab_widget = widget_stack.add();