Skip to content

Commit

Permalink
Algos: improve error messages of getCmdOutput (#1107)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui authored Jan 20, 2025
1 parent aa832cd commit 7d15050
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Algos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,28 @@ getCmdOutput(const Command& cmd, const std::size_t retry) noexcept {
logger::trace("Running `{}`", cmd.toString());

int exitCode = EXIT_SUCCESS;
std::string stdErr;
int waitTime = 1;
for (std::size_t i = 0; i < retry; ++i) {
const auto cmdOut = Try(cmd.output());
if (cmdOut.exitCode == EXIT_SUCCESS) {
return Ok(cmdOut.stdOut);
}
exitCode = cmdOut.exitCode;
stdErr = cmdOut.stdErr;

// Sleep for an exponential backoff.
std::this_thread::sleep_for(std::chrono::seconds(waitTime));
waitTime *= 2;
}
Bail("Command `{}` failed with exit code {}", cmd.toString(), exitCode);

return Result<std::string>(Err(Anyhow(
"Command `{}` failed with exit code {}",
cmd.toString(), exitCode
)))
.with_context([stdErr = std::move(stdErr)] {
return anyhow::anyhow(stdErr);
});
}

bool
Expand Down

0 comments on commit 7d15050

Please sign in to comment.