Let SubprocessError inherit std::runtime_error
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 10 Jun 2017 03:19:01 +0000 (20:19 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sat, 10 Jun 2017 03:19:47 +0000 (20:19 -0700)
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
folly/Subprocess.h

index b2d9cca0d21c05385298052f45fb6f56dd2e3494..52dad4350bc91eec3f15eb0a21be3af18e3f5ca6 100644 (file)
@@ -112,20 +112,25 @@ std::string ProcessReturnCode::str() const {
 }
 
 CalledProcessError::CalledProcessError(ProcessReturnCode rc)
 }
 
 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<std::string>(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<std::string>(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
 namespace {
 
 // Copy pointers to the given strings in a format suitable for posix_spawn
index f1dd2ecd9b4548189b0db41a907e7c59dba9544a..ea2a062afd16dd12acbc97ebfefa708c52d6070c 100644 (file)
@@ -209,7 +209,10 @@ class ProcessReturnCode {
 /**
  * Base exception thrown by the Subprocess methods.
  */
 /**
  * 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.
 
 /**
  * Exception thrown by *Checked methods of Subprocess.
@@ -218,11 +221,9 @@ class CalledProcessError : public SubprocessError {
  public:
   explicit CalledProcessError(ProcessReturnCode rc);
   ~CalledProcessError() throw() override = default;
  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_;
   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;
  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_;
   int errnoValue() const { return errnoValue_; }
 
  private:
   int errnoValue_;
-  std::string what_;
 };
 
 /**
 };
 
 /**