Skip to content

Commit 96b5de6

Browse files
authored
Report errors in file input/output operations (#137)
Throw `std::filesystem::filesystem_error` from `file::read`, `file::write` and `file::append`
1 parent 708e3fe commit 96b5de6

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

include/tmp/file

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ public:
7474

7575
/// Reads the entire contents of this file
7676
/// @returns A string with this file contents
77+
/// @throws std::filesystem::filesystem_error if cannot read the file contents
7778
std::string read() const;
7879

7980
/// Writes the given content to this file discarding any previous content
8081
/// @param content A string to write to this file
82+
/// @throws std::filesystem::filesystem_error if cannot write to the file
8183
void write(std::string_view content) const;
8284

8385
/// Appends the given content to the end of this file
8486
/// @param content A string to append to this file
87+
/// @throws std::filesystem::filesystem_error if cannot append to the file
8588
void append(std::string_view content) const;
8689

8790
/// Returns the input stream to this file

src/file.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,36 @@ file file::copy(const fs::path& path, std::string_view label,
9191
}
9292

9393
std::string file::read() const {
94-
std::ifstream stream = input_stream();
95-
return std::string(std::istreambuf_iterator<char>(stream), {});
94+
try {
95+
std::ifstream stream = input_stream();
96+
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
97+
98+
return std::string(std::istreambuf_iterator(stream), {});
99+
} catch (const std::ios::failure& err) {
100+
throw fs::filesystem_error("Cannot read a temporary file", err.code());
101+
}
96102
}
97103

98104
void file::write(std::string_view content) const {
99-
output_stream(std::ios::trunc) << content;
105+
try {
106+
std::ofstream stream = output_stream(std::ios::trunc);
107+
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
108+
109+
stream << content;
110+
} catch (const std::ios::failure& err) {
111+
throw fs::filesystem_error("Cannot write to a temporary file", err.code());
112+
}
100113
}
101114

102115
void file::append(std::string_view content) const {
103-
output_stream(std::ios::app) << content;
116+
try {
117+
std::ofstream stream = output_stream(std::ios::app);
118+
stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
119+
120+
stream << content;
121+
} catch (const std::ios::failure& err) {
122+
throw fs::filesystem_error("Cannot write to a temporary file", err.code());
123+
}
104124
}
105125

106126
std::ifstream file::input_stream() const {

tests/file.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ TEST(file, read_text) {
130130
EXPECT_EQ(tmpfile.read(), "Hello,\nworld!\n");
131131
}
132132

133+
/// Tests file reading error reporting
134+
TEST(file, read_error) {
135+
file tmpfile = file();
136+
fs::remove(tmpfile);
137+
138+
EXPECT_THROW(tmpfile.read(), fs::filesystem_error);
139+
}
140+
133141
/// Tests binary file writing
134142
TEST(file, write_binary) {
135143
file tmpfile = file();
@@ -178,6 +186,15 @@ TEST(file, write_text) {
178186
}
179187
}
180188

189+
/// Tests file writing error reporting
190+
TEST(file, write_error) {
191+
file tmpfile = file();
192+
fs::permissions(tmpfile.path(), fs::perms::none);
193+
194+
EXPECT_THROW(tmpfile.write("Hello!"), fs::filesystem_error);
195+
fs::permissions(tmpfile.path(), fs::perms::all);
196+
}
197+
181198
/// Tests binary file appending
182199
TEST(file, append_binary) {
183200
file tmpfile = file();
@@ -230,6 +247,15 @@ TEST(file, append_text) {
230247
}
231248
}
232249

250+
/// Tests file appending error reporting
251+
TEST(file, append_error) {
252+
file tmpfile = file();
253+
fs::permissions(tmpfile.path(), fs::perms::none);
254+
255+
EXPECT_THROW(tmpfile.append("world!"), fs::filesystem_error);
256+
fs::permissions(tmpfile.path(), fs::perms::all);
257+
}
258+
233259
/// Tests binary file reading from input_stream
234260
TEST(file, input_stream_binary) {
235261
file tmpfile = file();

0 commit comments

Comments
 (0)