From fbe8f185b551c2149c188686e9d3bee642a52e7d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Nov 2021 13:41:23 +0100 Subject: [PATCH] Profiler: Replace Result use with ErrorOr --- Userland/DevTools/Profiler/Profile.cpp | 17 ++++++++--------- Userland/DevTools/Profiler/Profile.h | 3 +-- Userland/DevTools/Profiler/main.cpp | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Userland/DevTools/Profiler/Profile.cpp b/Userland/DevTools/Profiler/Profile.cpp index 0fdc742651..bf4505a904 100644 --- a/Userland/DevTools/Profiler/Profile.cpp +++ b/Userland/DevTools/Profiler/Profile.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -214,15 +215,13 @@ void Profile::rebuild_tree() Optional g_kernel_debuginfo_object; OwnPtr g_kernel_debug_info; -Result, String> Profile::load_from_perfcore_file(const StringView& path) +ErrorOr> Profile::load_from_perfcore_file(const StringView& path) { - auto file = Core::File::construct(path); - if (!file->open(Core::OpenMode::ReadOnly)) - return String::formatted("Unable to open {}, error: {}", path, file->error_string()); + auto file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); auto json = JsonValue::from_string(file->read_all()); if (!json.has_value() || !json.value().is_object()) - return String { "Invalid perfcore format (not a JSON object)" }; + return Error::from_string_literal("Invalid perfcore format (not a JSON object)"sv); auto& object = json.value().as_object(); @@ -237,7 +236,7 @@ Result, String> Profile::load_from_perfcore_file(const St auto strings_value = object.get_ptr("strings"sv); if (!strings_value || !strings_value->is_array()) - return String { "Malformed profile (strings is not an array)" }; + return Error::from_string_literal("Malformed profile (strings is not an array)"sv); HashMap profile_strings; for (FlatPtr string_id = 0; string_id < strings_value->as_array().size(); ++string_id) { @@ -247,7 +246,7 @@ Result, String> Profile::load_from_perfcore_file(const St auto events_value = object.get_ptr("events"); if (!events_value || !events_value->is_array()) - return String { "Malformed profile (events is not an array)" }; + return Error::from_string_literal("Malformed profile (events is not an array)"sv); auto& perf_events = events_value->as_array(); @@ -419,7 +418,7 @@ Result, String> Profile::load_from_perfcore_file(const St } if (events.is_empty()) - return String { "No events captured (targeted process was never on CPU)" }; + return Error::from_string_literal("No events captured (targeted process was never on CPU)"sv); quick_sort(all_processes, [](auto& a, auto& b) { if (a.pid == b.pid) @@ -432,7 +431,7 @@ Result, String> Profile::load_from_perfcore_file(const St for (auto& it : all_processes) processes.append(move(it)); - return adopt_own(*new Profile(move(processes), move(events))); + return adopt_nonnull_own_or_enomem(new (nothrow) Profile(move(processes), move(events))); } void ProfileNode::sort_children() diff --git a/Userland/DevTools/Profiler/Profile.h b/Userland/DevTools/Profiler/Profile.h index df89641da4..1dc895882c 100644 --- a/Userland/DevTools/Profiler/Profile.h +++ b/Userland/DevTools/Profiler/Profile.h @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -142,7 +141,7 @@ struct ProcessFilter { class Profile { public: - static Result, String> load_from_perfcore_file(const StringView& path); + static ErrorOr> load_from_perfcore_file(const StringView& path); GUI::Model& model(); GUI::Model& samples_model(); diff --git a/Userland/DevTools/Profiler/main.cpp b/Userland/DevTools/Profiler/main.cpp index e87cb534ff..ac3e4e2991 100644 --- a/Userland/DevTools/Profiler/main.cpp +++ b/Userland/DevTools/Profiler/main.cpp @@ -68,7 +68,7 @@ int main(int argc, char** argv) auto profile_or_error = Profile::load_from_perfcore_file(perfcore_file); if (profile_or_error.is_error()) { - GUI::MessageBox::show(nullptr, profile_or_error.error(), "Profiler", GUI::MessageBox::Type::Error); + GUI::MessageBox::show(nullptr, String::formatted("{}", profile_or_error.error()), "Profiler", GUI::MessageBox::Type::Error); return 0; }