Skip to content

Commit

Permalink
Make file-util error messages more useful.
Browse files Browse the repository at this point in the history
 * Canonicalize one instance of creating an error message from fs::error
 * Filesystem messages: if there is an empty filename, explicitly show that.
 * UpwardFileSearch: provide more details in the emitted status.
  • Loading branch information
hzeller committed Jan 28, 2024
1 parent d3e3ea6 commit 74ae2d7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions common/util/file_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ static absl::Status CreateErrorStatusFromSysError(absl::string_view filename,
const char *fallback_msg) {
const char *const system_msg =
sys_error == 0 ? fallback_msg : strerror(sys_error);
const std::string msg = filename.empty()
? std::string{system_msg}
: absl::StrCat(filename, ": ", system_msg);
if (filename.empty()) filename = "<empty-filename>";
const std::string msg = absl::StrCat(filename, ": ", system_msg);
switch (sys_error) {
case EPERM:
case EACCES:
Expand Down Expand Up @@ -127,15 +126,17 @@ absl::Status UpwardFileSearch(absl::string_view start,
if (one_up == probe_dir) break;
probe_dir = one_up;
}
return absl::NotFoundError("No matching file found.");
return absl::NotFoundError(absl::StrCat("UpwardFileSearch: starting from '",
start, "', no file '", filename,
"' found'"));
}

absl::Status FileExists(const std::string &filename) {
std::error_code err;
fs::file_status stat = fs::status(filename, err);

if (err.value() != 0) {
return absl::NotFoundError(absl::StrCat(filename, ": ", err.message()));
return CreateErrorStatusFromErr(filename, err, "file exists check");
}

if (fs::is_regular_file(stat) || fs::is_fifo(stat)) {
Expand Down

0 comments on commit 74ae2d7

Please sign in to comment.