]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Util: Don’t include path in error description from read_file/write_file
authorThomas Otto <thomas.otto@pdv-fs.de>
Thu, 19 Mar 2020 21:41:55 +0000 (22:41 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 16 Apr 2020 20:33:25 +0000 (22:33 +0200)
By only returning the error description the caller is more flexible when
composing their own error message.

src/Util.cpp
src/Util.hpp
unittest/test_AtomicFile.cpp
unittest/test_Util.cpp

index dff944d84c09410370d4c929403e4cbf138b98b1..5a2adb655855bde6b3a8e7653b30c34a2ccbfc3e 100644 (file)
@@ -461,7 +461,7 @@ read_file(const std::string& path)
 {
   std::ifstream file(path);
   if (!file) {
-    throw Error(fmt::format("{}: {}", path, strerror(errno)));
+    throw Error(strerror(errno));
   }
   return std::string(std::istreambuf_iterator<char>(file),
                      std::istreambuf_iterator<char>());
@@ -568,7 +568,7 @@ write_file(const std::string& path, const std::string& data, bool binary)
   std::ofstream file(path,
                      binary ? std::ios::out | std::ios::binary : std::ios::out);
   if (!file) {
-    throw Error(fmt::format("{}: {}", path, strerror(errno)));
+    throw Error(strerror(errno));
   }
   file << data;
 }
index 26d3af556f422d5a4d4a9f7cf43ea91e9fa32703..62f290bafb673edc76e1e377a2b2eb1d79a03170 100644 (file)
@@ -220,7 +220,8 @@ int parse_int(const std::string& value);
 
 // Read file data as a string.
 //
-// Throws Error on error.
+// Throws `Error` on error. The description contains the error message without
+// the path.
 std::string read_file(const std::string& path);
 
 #ifndef _WIN32
@@ -250,7 +251,8 @@ strip_whitespace(const std::string& string);
 
 // Write file data from a string.
 //
-// Throws Error on error.
+// Throws `Error` on error. The description contains the error message without
+// the path.
 void write_file(const std::string& path,
                 const std::string& data,
                 bool binary = false);
index 3e07e1955ffce8cd749a78cfeea9532dba211ea1..71b19234fb399511822756a0942e02a3f86bae14 100644 (file)
@@ -42,5 +42,5 @@ TEST_CASE("Not committing")
     atomic_file.write("hello");
   }
   CHECK_THROWS_WITH(Util::read_file("test"),
-                    Equals("test: No such file or directory"));
+                    Equals("No such file or directory"));
 }
index bae8078621394f09589016167966b9879efa5878..0b0e9a0d00094704f889843af3ebb48028b206d2 100644 (file)
@@ -433,6 +433,12 @@ TEST_CASE("Util::read_file and Util::write_file")
   Util::write_file("test", "foo\nbar\n");
   std::string data = Util::read_file("test");
   CHECK(data == "foo\nbar\n");
+
+  CHECK_THROWS_WITH(Util::read_file("does/not/exist"),
+                    Equals("No such file or directory"));
+
+  CHECK_THROWS_WITH(Util::write_file("", "does/not/exist"),
+                    Equals("No such file or directory"));
 }
 
 TEST_CASE("Util::remove_extension")