diff --git a/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp b/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp index 132b428636..d95b2d9361 100644 --- a/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp +++ b/Userland/Libraries/LibGUI/CommonLocationsProvider.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -25,8 +26,11 @@ static void initialize_if_needed() auto user_config = DeprecatedString::formatted("{}/CommonLocations.json", Core::StandardPaths::config_directory()); if (Core::File::exists(user_config)) { - CommonLocationsProvider::load_from_json(user_config); - return; + auto maybe_error = CommonLocationsProvider::load_from_json(user_config); + if (!maybe_error.is_error()) + return; + dbgln("Unable to read Common Locations file: {}", maybe_error.error()); + dbgln("Using the default set instead."); } // Fallback : If the user doesn't have custom locations, use some default ones. @@ -36,23 +40,14 @@ static void initialize_if_needed() s_initialized = true; } -void CommonLocationsProvider::load_from_json(DeprecatedString const& json_path) +ErrorOr CommonLocationsProvider::load_from_json(StringView json_path) { - auto file = Core::File::construct(json_path); - if (!file->open(Core::OpenMode::ReadOnly)) { - dbgln("Unable to open {}", file->filename()); - return; - } - - auto json = JsonValue::from_string(file->read_all()); - if (json.is_error()) { - dbgln("Common locations file {} is not a valid JSON file.", file->filename()); - return; - } - if (!json.value().is_array()) { - dbgln("Common locations file {} should contain a JSON array.", file->filename()); - return; - } + auto file = TRY(Core::Stream::File::open(json_path, Core::Stream::OpenMode::Read)); + auto json = JsonValue::from_string(TRY(file->read_until_eof())); + if (json.is_error()) + return Error::from_string_literal("File is not a valid JSON"); + if (!json.value().is_array()) + return Error::from_string_literal("File must contain a JSON array"); s_common_locations.clear(); auto const& contents = json.value().as_array(); @@ -63,10 +58,11 @@ void CommonLocationsProvider::load_from_json(DeprecatedString const& json_path) auto entry = entry_value.as_object(); auto name = entry.get("name"sv).to_deprecated_string(); auto path = entry.get("path"sv).to_deprecated_string(); - s_common_locations.append({ name, path }); + TRY(s_common_locations.try_append({ name, path })); } s_initialized = true; + return {}; } Vector const& CommonLocationsProvider::common_locations() diff --git a/Userland/Libraries/LibGUI/CommonLocationsProvider.h b/Userland/Libraries/LibGUI/CommonLocationsProvider.h index 36d11302e0..b094f8b482 100644 --- a/Userland/Libraries/LibGUI/CommonLocationsProvider.h +++ b/Userland/Libraries/LibGUI/CommonLocationsProvider.h @@ -20,7 +20,7 @@ public: DeprecatedString path; }; - static void load_from_json(DeprecatedString const& json_path); + static ErrorOr load_from_json(StringView json_path); static Vector const& common_locations(); };