Skip to content

Commit

Permalink
Raise runtime_error on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kgadek committed Dec 2, 2021
1 parent a878991 commit 744822e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mst.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ int main(int argc, char *argv[]) {
mstch::map ctx{
{"include", mstch::lambda{ [](const std::string &filename) -> mstch::node {
std::ifstream file_fstream{ filename };
if((file_fstream.rdstate() & std::ifstream::failbit) != 0)
throw std::runtime_error("Can't open file: " + filename);
return std::string { std::istreambuf_iterator<char>{ file_fstream },
std::istreambuf_iterator<char>{} };
}}},
{"env", mstch::lambda{ [](const std::string &env_var_name) -> mstch::node {
if(char *env_var_value = std::getenv(env_var_name.c_str())) {
return std::string{env_var_value};
} else {
return std::string{};
throw std::runtime_error("Env var doesn't exist: " + env_var_name);
}
}}},
{"cmd", mstch::lambda{ [](const std::string &cmd) -> mstch::node {
Expand All @@ -36,7 +38,7 @@ int main(int argc, char *argv[]) {
std::string stdout;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if(!pipe)
return "Command \"" + cmd + "\" failed";
throw std::runtime_error("Command failed: " + cmd);
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
stdout += buffer.data();
return stdout;
Expand Down

0 comments on commit 744822e

Please sign in to comment.