Move exception from PostAnalysisQueryBuildTool to BuildTool.

This breaks a dependency cycle between BuildTool and PostAnalysisQueryBuildTool.

PiperOrigin-RevId: 305334630
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/AqueryBuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/AqueryBuildTool.java
index 8f75c3d..de3e3fc 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/AqueryBuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/AqueryBuildTool.java
@@ -113,7 +113,7 @@
         }
       }
       return BlazeCommandResult.exitCode(ExitCode.SUCCESS);
-    } catch (CommandLineException | CommandLineExpansionException e) {
+    } catch (CommandLineExpansionException | CommandLineException e) {
       env.getReporter().handle(Event.error("Error while parsing command: " + e.getMessage()));
       return BlazeCommandResult.exitCode(ExitCode.COMMAND_LINE_ERROR);
     } catch (IOException e) {
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
index 36ca625..ab878e9 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
@@ -29,7 +29,6 @@
 import com.google.devtools.build.lib.analysis.config.BuildOptions;
 import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
 import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil;
-import com.google.devtools.build.lib.buildtool.PostAnalysisQueryBuildTool.PostAnalysisQueryCommandLineException;
 import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
 import com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent;
 import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
@@ -106,7 +105,7 @@
   public void buildTargets(BuildRequest request, BuildResult result, TargetValidator validator)
       throws BuildFailedException, InterruptedException, ViewCreationFailedException,
           TargetParsingException, LoadingFailedException, AbruptExitException,
-          InvalidConfigurationException, TestExecException, PostAnalysisQueryCommandLineException {
+          InvalidConfigurationException, TestExecException, QueryCommandLineException {
     try (SilentCloseable c = Profiler.instance().profile("validateOptions")) {
       validateOptions(request);
     }
@@ -246,12 +245,8 @@
    * This class is meant to be overridden by classes that want to perform the Analysis phase and
    * then process the results in some interesting way. See {@link CqueryBuildTool} as an example.
    */
-  protected void postProcessAnalysisResult(
-      BuildRequest request,
-      AnalysisResult analysisResult)
-      throws InterruptedException, ViewCreationFailedException,
-          PostAnalysisQueryCommandLineException {
-  }
+  protected void postProcessAnalysisResult(BuildRequest request, AnalysisResult analysisResult)
+      throws InterruptedException, ViewCreationFailedException, QueryCommandLineException {}
 
   private void reportExceptionError(Exception e) {
     if (e.getMessage() != null) {
@@ -318,7 +313,7 @@
     } catch (TargetParsingException | LoadingFailedException | ViewCreationFailedException e) {
       detailedExitCode = DetailedExitCode.justExitCode(ExitCode.PARSING_FAILURE);
       reportExceptionError(e);
-    } catch (PostAnalysisQueryCommandLineException e) {
+    } catch (QueryCommandLineException e) {
       detailedExitCode = DetailedExitCode.justExitCode(ExitCode.COMMAND_LINE_ERROR);
       reportExceptionError(e);
     } catch (TestExecException e) {
@@ -446,4 +441,11 @@
   private Reporter getReporter() {
     return env.getReporter();
   }
+
+  /** Exceptions in parsing the supplied query options. */
+  protected static class QueryCommandLineException extends Exception {
+    QueryCommandLineException(String message) {
+      super(message);
+    }
+  }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java
index 4104002..8ccf700 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryBuildTool.java
@@ -25,7 +25,6 @@
 import com.google.devtools.build.lib.query2.engine.QueryExpression;
 import com.google.devtools.build.lib.runtime.CommandEnvironment;
 import com.google.devtools.build.lib.runtime.QueryRuntimeHelper;
-import com.google.devtools.build.lib.runtime.QueryRuntimeHelper.Factory.CommandLineException;
 import com.google.devtools.build.lib.skyframe.SkyframeExecutorWrappingWalkableGraph;
 import com.google.devtools.build.skyframe.SkyKey;
 import com.google.devtools.build.skyframe.WalkableGraph;
@@ -46,11 +45,8 @@
   }
 
   @Override
-  protected void postProcessAnalysisResult(
-      BuildRequest request,
-      AnalysisResult analysisResult)
-      throws InterruptedException, ViewCreationFailedException,
-          PostAnalysisQueryCommandLineException {
+  protected void postProcessAnalysisResult(BuildRequest request, AnalysisResult analysisResult)
+      throws InterruptedException, ViewCreationFailedException, QueryCommandLineException {
     // TODO: b/71905538 - this query will operate over the graph as constructed by analysis, but
     // will also pick up any nodes that are in the graph from prior builds. This makes the results
     // not reproducible at the level of a single command. Either tolerate, or wipe the analysis
@@ -58,7 +54,7 @@
     // (SkyframeExecutor#handleAnalysisInvalidatingChange should be sufficient).
     if (queryExpression != null) {
       if (!env.getSkyframeExecutor().tracksStateForIncrementality()) {
-        throw new PostAnalysisQueryCommandLineException(
+        throw new QueryCommandLineException(
             "Queries based on analysis results are not allowed "
                 + "if incrementality state is not being kept");
       }
@@ -76,8 +72,8 @@
           throw new ViewCreationFailedException("Error doing post analysis query", e);
         }
         env.getReporter().error(null, "Error doing post analysis query", e);
-      } catch (CommandLineException e) {
-        throw new PostAnalysisQueryCommandLineException(e.getMessage());
+      } catch (QueryRuntimeHelper.Factory.CommandLineException e) {
+        throw new QueryCommandLineException(e.getMessage());
       }
     }
   }
@@ -138,11 +134,4 @@
     }
     queryRuntimeHelper.afterQueryOutputIsWritten();
   }
-
-  /** Post analysis query specific command line exception. */
-  protected static class PostAnalysisQueryCommandLineException extends Exception {
-    PostAnalysisQueryCommandLineException(String message) {
-      super(message);
-    }
-  }
 }