Commit ee87051b authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Let SubprocessError inherit std::runtime_error

Summary:
[Folly] Let `SubprocessError` inherit `std::runtime_error`.

As an added bonus, this gives it `std::runtime_error`'s refcounted string for cheap copies.

Reviewed By: ericniebler

Differential Revision: D5216758

fbshipit-source-id: 43298e06f02cfd88abf2d73f7aa16117a6cb052b
parent d8ed7cbc
......@@ -112,19 +112,24 @@ std::string ProcessReturnCode::str() const {
}
CalledProcessError::CalledProcessError(ProcessReturnCode rc)
: returnCode_(rc),
what_(returnCode_.str()) {
: SubprocessError(rc.str()), returnCode_(rc) {}
static inline std::string toSubprocessSpawnErrorMessage(
char const* executable,
int errCode,
int errnoValue) {
auto prefix = errCode == kExecFailure ? "failed to execute "
: "error preparing to execute ";
return to<std::string>(prefix, executable, ": ", errnoStr(errnoValue));
}
SubprocessSpawnError::SubprocessSpawnError(const char* executable,
SubprocessSpawnError::SubprocessSpawnError(
const char* executable,
int errCode,
int errnoValue)
: errnoValue_(errnoValue),
what_(to<std::string>(errCode == kExecFailure ?
"failed to execute " :
"error preparing to execute ",
executable, ": ", errnoStr(errnoValue))) {
}
: SubprocessError(
toSubprocessSpawnErrorMessage(executable, errCode, errnoValue)),
errnoValue_(errnoValue) {}
namespace {
......
......@@ -209,7 +209,10 @@ class ProcessReturnCode {
/**
* Base exception thrown by the Subprocess methods.
*/
class SubprocessError : public std::exception {};
class SubprocessError : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
/**
* Exception thrown by *Checked methods of Subprocess.
......@@ -218,11 +221,9 @@ class CalledProcessError : public SubprocessError {
public:
explicit CalledProcessError(ProcessReturnCode rc);
~CalledProcessError() throw() override = default;
const char* what() const throw() override { return what_.c_str(); }
ProcessReturnCode returnCode() const { return returnCode_; }
private:
ProcessReturnCode returnCode_;
std::string what_;
};
/**
......@@ -232,12 +233,10 @@ class SubprocessSpawnError : public SubprocessError {
public:
SubprocessSpawnError(const char* executable, int errCode, int errnoValue);
~SubprocessSpawnError() throw() override = default;
const char* what() const throw() override { return what_.c_str(); }
int errnoValue() const { return errnoValue_; }
private:
int errnoValue_;
std::string what_;
};
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment