Skip to content

Commit e76a04e

Browse files
committed
Distinguish file-not-found errors from other errors
To implement config file search, I need to know if a file couldn't be opened because it doesn't exist or if a file couldn't be opened because of another reason. Have read_file return more precise information about any errors encountered. This feature isn't used anywhere yet (except tests).
1 parent 33f9b10 commit e76a04e

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/file.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,11 @@ read_file_result read_file(const char *path) {
205205
/*hTemplateFile=*/nullptr);
206206
if (handle == INVALID_HANDLE_VALUE) {
207207
DWORD error = ::GetLastError();
208-
return read_file_result::failure(std::string("failed to open ") + path +
209-
": " + windows_error_message(error));
208+
read_file_result result =
209+
read_file_result::failure(std::string("failed to open ") + path + ": " +
210+
windows_error_message(error));
211+
result.is_not_found_error = error == ERROR_FILE_NOT_FOUND;
212+
return result;
210213
}
211214
windows_handle_file file(handle);
212215
return read_file(path, file.ref());
@@ -223,8 +226,10 @@ read_file_result read_file(const char *path) {
223226
int fd = ::open(path, O_CLOEXEC | O_RDONLY);
224227
if (fd == -1) {
225228
int error = errno;
226-
return read_file_result::failure(std::string("failed to open ") + path +
227-
": " + std::strerror(error));
229+
read_file_result result = read_file_result::failure(
230+
std::string("failed to open ") + path + ": " + std::strerror(error));
231+
result.is_not_found_error = error == ENOENT;
232+
return result;
228233
}
229234
posix_fd_file file(fd);
230235
return read_file(path, file.ref());

src/quick-lint-js/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace quick_lint_js {
1313
struct read_file_result {
1414
padded_string content;
1515
std::string error;
16+
bool is_not_found_error = false;
1617

1718
bool ok() const noexcept { return this->error.empty(); }
1819
void exit_if_not_ok() const;

test/test-file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ TEST_F(test_file, read_non_existing_file) {
7171

7272
read_file_result file_content = read_file(temp_file_path.c_str());
7373
EXPECT_FALSE(file_content.ok());
74+
EXPECT_TRUE(file_content.is_not_found_error);
7475
EXPECT_THAT(file_content.error, HasSubstr("does-not-exist.js"));
7576
EXPECT_THAT(file_content.error,
7677
AnyOf(HasSubstr("No such file"), HasSubstr("cannot find")));
@@ -81,6 +82,7 @@ TEST_F(test_file, read_directory) {
8182

8283
read_file_result file_content = read_file(temp_file_path.c_str());
8384
EXPECT_FALSE(file_content.ok());
85+
EXPECT_FALSE(file_content.is_not_found_error);
8486
EXPECT_THAT(
8587
file_content.error,
8688
testing::AnyOf(

0 commit comments

Comments
 (0)