From: Joel Rosdahl Date: Thu, 20 Jul 2023 07:14:53 +0000 (+0200) Subject: refactor: Use util::{throw_on_error,value_or_throw} X-Git-Tag: v4.9~97 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2ba3da5eca2eccefe07f2a5bfbfe885547588442;p=thirdparty%2Fccache.git refactor: Use util::{throw_on_error,value_or_throw} --- diff --git a/src/Config.cpp b/src/Config.cpp index 3080fa7bc..15b2c3d13 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -911,11 +911,9 @@ Config::set_value_in_file(const std::string& path, const auto st = Stat::stat(resolved_path); if (!st) { core::ensure_dir_exists(Util::dir_name(resolved_path)); - const auto result = util::write_file(resolved_path, ""); - if (!result) { - throw core::Error( - FMT("failed to write to {}: {}", resolved_path, result.error())); - } + util::throw_on_error( + util::write_file(resolved_path, ""), + FMT("failed to write to {}: ", resolved_path)); } AtomicFile output(resolved_path, AtomicFile::Mode::text); @@ -1157,11 +1155,7 @@ Config::set_item(const std::string& key, case ConfigItem::umask: if (!value.empty()) { - const auto umask = util::parse_umask(value); - if (!umask) { - throw core::Error(umask.error()); - } - m_umask = *umask; + m_umask = util::value_or_throw(util::parse_umask(value)); } break; } diff --git a/src/Util.cpp b/src/Util.cpp index df42f5c1f..da87144b6 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -495,11 +496,9 @@ send_to_fd(const Context& ctx, std::string_view text, int fd) text_to_send = modified_text; } - const auto result = - util::write_fd(fd, text_to_send.data(), text_to_send.length()); - if (!result) { - throw core::Error(FMT("Failed to write to {}: {}", fd, result.error())); - } + util::throw_on_error( + util::write_fd(fd, text_to_send.data(), text_to_send.length()), + FMT("Failed to write to fd {}: ", fd)); } std::string diff --git a/src/core/CacheEntry.cpp b/src/core/CacheEntry.cpp index 26595edcc..b0b5c165b 100644 --- a/src/core/CacheEntry.cpp +++ b/src/core/CacheEntry.cpp @@ -106,11 +106,8 @@ CacheEntry::Header::Header(nonstd::span data) CacheEntry::Header::Header(const std::string& path) { - const auto data = util::read_file_part(path, 0, 1000); - if (!data) { - throw core::Error(data.error()); - } - parse(*data); + parse(util::value_or_throw( + util::read_file_part(path, 0, 1000))); } std::string