]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Extract common "receive error message" code into helper
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 14 May 2026 12:10:45 +0000 (14:10 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 14 May 2026 12:10:45 +0000 (14:10 +0200)
src/ccache/storage/remote/client.cpp
src/ccache/storage/remote/client.hpp

index 8fb8d90e9ea156100a496e714552a10e56647c55..119d4a408781075635121e2beaa408733dfeb214 100644 (file)
@@ -323,6 +323,14 @@ Client::receive_u64()
   return value;
 }
 
+tl::expected<std::string, Client::Error>
+Client::receive_error_string()
+{
+  TRY_ASSIGN(uint8_t msg_len, receive_u8());
+  TRY_ASSIGN(auto msg_bytes, receive_bytes(msg_len));
+  return std::string(msg_bytes.begin(), msg_bytes.end());
+}
+
 tl::expected<std::optional<util::Bytes>, Client::Error>
 Client::receive_response_get()
 {
@@ -340,10 +348,8 @@ Client::receive_response_get()
     return std::nullopt;
 
   case Status::error: {
-    TRY_ASSIGN(uint8_t msg_len, receive_u8());
-    TRY_ASSIGN(auto msg_bytes, receive_bytes(msg_len));
-    std::string error_msg(msg_bytes.begin(), msg_bytes.end());
-    return tl::unexpected(Error(Failure::error, error_msg));
+    TRY_ASSIGN(auto err_msg, receive_error_string());
+    return tl::unexpected(Error(Failure::error, err_msg));
   }
 
   default:
@@ -366,10 +372,8 @@ Client::receive_response_bool()
     return false;
 
   case Status::error: {
-    TRY_ASSIGN(uint8_t msg_len, receive_u8());
-    TRY_ASSIGN(auto msg_bytes, receive_bytes(msg_len));
-    std::string error_msg(msg_bytes.begin(), msg_bytes.end());
-    return tl::unexpected(Error(Failure::error, error_msg));
+    TRY_ASSIGN(auto err_msg, receive_error_string());
+    return tl::unexpected(Error(Failure::error, err_msg));
   }
 
   default:
@@ -393,10 +397,8 @@ Client::receive_response_void()
     return {};
 
   case Status::error: {
-    TRY_ASSIGN(uint8_t msg_len, receive_u8());
-    TRY_ASSIGN(auto msg_bytes, receive_bytes(msg_len));
-    std::string error_msg(msg_bytes.begin(), msg_bytes.end());
-    return tl::unexpected(Error(Failure::error, error_msg));
+    TRY_ASSIGN(auto err_msg, receive_error_string());
+    return tl::unexpected(Error(Failure::error, err_msg));
   }
 
   default:
index 5935067c400e3f971aa0073d4629ce52fe1de414..4091c293e84db223628edcf3cba18d85579c14b8 100644 (file)
@@ -124,6 +124,7 @@ private:
   tl::expected<util::Bytes, Error> receive_bytes(size_t count);
   tl::expected<uint8_t, Error> receive_u8();
   tl::expected<uint64_t, Error> receive_u64();
+  tl::expected<std::string, Error> receive_error_string();
   tl::expected<std::optional<util::Bytes>, Error> receive_response_get();
   tl::expected<bool, Error> receive_response_bool();
   tl::expected<void, Error> receive_response_void();