From ee87051bba16d2c98a1871450ff390924d66a4df Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Fri, 9 Jun 2017 20:19:01 -0700 Subject: [PATCH] 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 --- folly/Subprocess.cpp | 27 ++++++++++++++++----------- folly/Subprocess.h | 9 ++++----- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index b2d9cca0..52dad435 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -112,20 +112,25 @@ std::string ProcessReturnCode::str() const { } CalledProcessError::CalledProcessError(ProcessReturnCode rc) - : returnCode_(rc), - what_(returnCode_.str()) { -} + : SubprocessError(rc.str()), returnCode_(rc) {} -SubprocessSpawnError::SubprocessSpawnError(const char* executable, - int errCode, - int errnoValue) - : errnoValue_(errnoValue), - what_(to(errCode == kExecFailure ? - "failed to execute " : - "error preparing to execute ", - executable, ": ", errnoStr(errnoValue))) { +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(prefix, executable, ": ", errnoStr(errnoValue)); } +SubprocessSpawnError::SubprocessSpawnError( + const char* executable, + int errCode, + int errnoValue) + : SubprocessError( + toSubprocessSpawnErrorMessage(executable, errCode, errnoValue)), + errnoValue_(errnoValue) {} + namespace { // Copy pointers to the given strings in a format suitable for posix_spawn diff --git a/folly/Subprocess.h b/folly/Subprocess.h index f1dd2ecd..ea2a062a 100644 --- a/folly/Subprocess.h +++ b/folly/Subprocess.h @@ -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_; }; /** -- 2.34.1