]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use util::{throw_on_error,value_or_throw}
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 20 Jul 2023 07:14:53 +0000 (09:14 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 25 Jul 2023 06:54:46 +0000 (08:54 +0200)
src/Config.cpp
src/Util.cpp
src/core/CacheEntry.cpp

index 3080fa7bc759bdeaf400ff13bd089da07fbe78de..15b2c3d13bf5c8f38702ede23f2ea8fad2f04a05 100644 (file)
@@ -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<core::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<core::Error>(util::parse_umask(value));
     }
     break;
   }
index df42f5c1fc207e4a4317d1c006a3a7f7dd381d0a..da87144b630aaf008c03bda9079a2e7f0296ef48 100644 (file)
@@ -31,6 +31,7 @@
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
+#include <util/expected.hpp>
 #include <util/file.hpp>
 #include <util/filesystem.hpp>
 #include <util/path.hpp>
@@ -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<core::Error>(
+    util::write_fd(fd, text_to_send.data(), text_to_send.length()),
+    FMT("Failed to write to fd {}: ", fd));
 }
 
 std::string
index 26595edcc530b153232451c9eb55029f9119c57d..b0b5c165bc89528e5584c8bd23a4e9824cd7266e 100644 (file)
@@ -106,11 +106,8 @@ CacheEntry::Header::Header(nonstd::span<const uint8_t> data)
 
 CacheEntry::Header::Header(const std::string& path)
 {
-  const auto data = util::read_file_part<util::Bytes>(path, 0, 1000);
-  if (!data) {
-    throw core::Error(data.error());
-  }
-  parse(*data);
+  parse(util::value_or_throw<core::Error>(
+    util::read_file_part<util::Bytes>(path, 0, 1000)));
 }
 
 std::string