public static function addLocationToBuildException(BuildException $ex, Location $newLocation)
    {
        if ($ex->getLocation() === null || $ex->getMessage() === null) {
            return $ex;
        }
        $errorMessage = sprintf(
            "The following error occurred while executing this line:%s%s %s%s",
            PHP_EOL,
            $ex->getLocation(),
            $ex->getMessage(),
            PHP_EOL
        );
        if ($ex instanceof ExitStatusException) {
            $exitStatus = $ex->getCode();
            if ($newLocation === null) {
                return new ExitStatusException($errorMessage, $exitStatus);
            }
            return new ExitStatusException($errorMessage, $exitStatus, $newLocation);
        }

        return new BuildException($errorMessage, $ex, $newLocation);
    }