Skip to content

Commit 4ab073b

Browse files
refactor(pipe): attach error code to exceptions via std::system_error
1 parent 2a4eec4 commit 4ab073b

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

source/matplot/util/common.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ namespace matplot {
5050
iequals(str, "no");
5151
}
5252

53+
struct pipe_deleter {
54+
int operator()(FILE* pipe) const {
55+
if (int status = PCLOSE(pipe); status != -1)
56+
return status;
57+
throw std::system_error{errno, std::system_category(), "pclose"};
58+
}
59+
};
60+
5361
std::string run_and_get_output(const std::string &cmd) {
54-
std::unique_ptr<FILE, int (*)(FILE *)> pipe(POPEN(cmd.c_str(), "r"),
55-
PCLOSE);
62+
std::unique_ptr<FILE, pipe_deleter> pipe(POPEN(cmd.c_str(), "r"));
5663
if (!pipe) {
57-
throw std::runtime_error("popen() failed!");
64+
throw std::system_error{errno, std::system_category(), cmd};
5865
}
5966
std::array<char, 128> buffer{};
6067
std::string result;
@@ -363,7 +370,7 @@ namespace matplot {
363370
std::string fileread(const std::string &filename) {
364371
std::ifstream t(filename);
365372
if (!t) {
366-
throw std::runtime_error("Cannot open the file " + filename);
373+
throw std::system_error(errno, std::system_category(), filename);
367374
}
368375
std::string str((std::istreambuf_iterator<char>(t)),
369376
std::istreambuf_iterator<char>());

0 commit comments

Comments
 (0)