Unify command error handling

BUG=chromium:773875
TEST=unit test

Change-Id: Ibe32309c021d72e08cecc7d6830756fa1503e809
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1695801
Reviewed-by: Tobias Bosch <tbosch@google.com>
Tested-by: Tobias Bosch <tbosch@google.com>
diff --git a/compiler_wrapper/compiler_wrapper.go b/compiler_wrapper/compiler_wrapper.go
index 3f94e18..e24faef 100644
--- a/compiler_wrapper/compiler_wrapper.go
+++ b/compiler_wrapper/compiler_wrapper.go
@@ -4,8 +4,6 @@
 	"fmt"
 	"io"
 	"path/filepath"
-	"strings"
-	"syscall"
 )
 
 func callCompiler(env env, cfg *config, inputCmd *command) int {
@@ -79,18 +77,9 @@
 	if shouldForceDisableWError(env) {
 		return doubleBuildWithWNoError(env, cfg, compilerCmd)
 	}
-	if err := env.exec(compilerCmd); err != nil {
-		if userErr, ok := getCCacheError(compilerCmd, err); ok {
-			return 0, userErr
-		}
-		// Note: This case only happens when the underlying env is not
-		// really doing an exec, e.g. commandRecordingEnv.
-		if exitCode, ok := getExitCode(err); ok {
-			return exitCode, nil
-		}
-		return exitCode, wrapErrorwithSourceLocf(err, "failed to execute %#v", compilerCmd)
-	}
-	return 0, err
+	// Note: We return an exit code only if the underlying env is not
+	// really doing an exec, e.g. commandRecordingEnv.
+	return wrapSubprocessErrorWithSourceLoc(compilerCmd, env.exec(compilerCmd))
 }
 
 func calcClangCommand(forceLocal bool, builder *commandBuilder) (*command, error) {
@@ -142,17 +131,6 @@
 	return filepath.Dir(evaledCmdPath), nil
 }
 
-func getCCacheError(compilerCmd *command, compilerCmdErr error) (ccacheErr userError, ok bool) {
-	if en, ok := compilerCmdErr.(syscall.Errno); ok && en == syscall.ENOENT &&
-		strings.Contains(compilerCmd.path, "ccache") {
-		ccacheErr =
-			newUserErrorf("ccache not found under %s. Please install it",
-				compilerCmd.path)
-		return ccacheErr, ok
-	}
-	return ccacheErr, false
-}
-
 func printCompilerError(writer io.Writer, compilerErr error) {
 	if _, ok := compilerErr.(userError); ok {
 		fmt.Fprintf(writer, "%s\n", compilerErr)