feat: add try-catch to some usages of readLine

This commit is contained in:
Nicolas Lenz 2024-08-21 10:25:35 +02:00
parent ac6974777e
commit e6e97811de
Signed by: eisfunke
SSH key fingerprint: SHA256:cSUjDeGtM/lFXeIi40eIXMC3yPkqtyRNV1P5KI+IdgU
2 changed files with 16 additions and 2 deletions

View file

@ -558,7 +558,14 @@ std::optional<char> ProgressBar::ask(std::string_view msg)
auto state(state_.lock());
if (state->paused > 0 || !isatty(STDIN_FILENO)) return {};
std::cerr << fmt("\r\e[K%s ", msg);
auto s = trim(readLine(STDIN_FILENO));
auto s = [&]() {
try {
return trim(readLine(STDIN_FILENO));
} catch (Error & e) {
e.addTrace({}, "while trying to read from STDIN to ask something");
throw;
}
}();
if (s.size() != 1) return {};
draw(*state, {});
return s[0];

View file

@ -930,7 +930,14 @@ Pid LinuxLocalDerivationGoal::startChild(std::function<void()> openSlave)
userNamespaceSync.writeSide.reset();
});
auto ss = tokenizeString<std::vector<std::string>>(readLine(sendPid.readSide.get()));
auto ss = [&]() {
try {
return tokenizeString<std::vector<std::string>>(readLine(sendPid.readSide.get()));
} catch (Error & e) {
e.addTrace({}, "while... how am I supposed to know?");
throw;
}
}();
assert(ss.size() == 1);
Pid pid = Pid{string2Int<pid_t>(ss[0]).value()};