Add FailureDetail to TargetParsingException as a requirement.

Update all instances of TargetParsingException to include FailureDetail code. New failure detail codes are added to the TargetPatterns category.

RELNOTES: None.
PiperOrigin-RevId: 324601642
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
index 964c5ad..6afe5e9 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
@@ -27,6 +27,7 @@
 import com.google.devtools.build.lib.cmdline.LabelValidator.BadLabelException;
 import com.google.devtools.build.lib.cmdline.LabelValidator.PackageAndTarget;
 import com.google.devtools.build.lib.concurrent.BatchCallback;
+import com.google.devtools.build.lib.server.FailureDetails.TargetPatterns;
 import com.google.devtools.build.lib.util.StringUtilities;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import com.google.errorprone.annotations.CheckReturnValue;
@@ -413,7 +414,9 @@
           }
         }
 
-        throw new TargetParsingException("couldn't determine target from filename '" + path + "'");
+        throw new TargetParsingException(
+            "couldn't determine target from filename '" + path + "'",
+            TargetPatterns.Code.CANNOT_DETERMINE_TARGET_FROM_FILENAME);
       }
     }
 
@@ -785,20 +788,25 @@
       if (includesRepo) {
         int pkgStart = pattern.indexOf("//");
         if (pkgStart < 0) {
-          throw new TargetParsingException("Couldn't find package in target " + pattern);
+          throw new TargetParsingException(
+              "Couldn't find package in target " + pattern, TargetPatterns.Code.PACKAGE_NOT_FOUND);
         }
         try {
           repository = RepositoryName.create(pattern.substring(0, pkgStart));
         } catch (LabelSyntaxException e) {
-          throw new TargetParsingException(e.getMessage());
+          throw new TargetParsingException(e.getMessage(), TargetPatterns.Code.LABEL_SYNTAX_ERROR);
         }
 
         pattern = pattern.substring(pkgStart);
       }
 
       if (!VALID_SLASH_PREFIX.matcher(pattern).lookingAt()) {
-        throw new TargetParsingException("not a valid absolute pattern (absolute target patterns "
-            + "must start with exactly two slashes): '" + pattern + "'");
+        throw new TargetParsingException(
+            "not a valid absolute pattern (absolute target patterns "
+                + "must start with exactly two slashes): '"
+                + pattern
+                + "'",
+            TargetPatterns.Code.ABSOLUTE_TARGET_PATTERN_INVALID);
       }
 
       final boolean wasOriginallyAbsolute = pattern.startsWith("//");
@@ -806,7 +814,9 @@
       pattern = absolutize(pattern).substring(2);
 
       if (pattern.isEmpty()) {
-        throw new TargetParsingException("the empty string is not a valid target");
+        throw new TargetParsingException(
+            "the empty string is not a valid target",
+            TargetPatterns.Code.TARGET_CANNOT_BE_EMPTY_STRING);
       }
 
       // Transform "/BUILD" suffix into ":BUILD" to accept //foo/bar/BUILD
@@ -824,8 +834,9 @@
       }
 
       if (packagePart.endsWith("/")) {
-        throw new TargetParsingException("The package part of '" + originalPattern
-            + "' should not end in a slash");
+        throw new TargetParsingException(
+            "The package part of '" + originalPattern + "' should not end in a slash",
+            TargetPatterns.Code.PACKAGE_PART_CANNOT_END_IN_SLASH);
       }
 
       if (repository == null) {
@@ -840,7 +851,8 @@
               repository.getName() + "//" + realPackagePart);
         } catch (LabelSyntaxException e) {
           throw new TargetParsingException(
-              "Invalid package name '" + realPackagePart + "': " + e.getMessage());
+              "Invalid package name '" + realPackagePart + "': " + e.getMessage(),
+              TargetPatterns.Code.LABEL_SYNTAX_ERROR);
         }
         if (targetPart.isEmpty() || ALL_RULES_IN_SUFFIXES.contains(targetPart)) {
           return new TargetsBelowDirectory(
@@ -857,7 +869,8 @@
           packageIdentifier = PackageIdentifier.parse(repository.getName() + "//" + packagePart);
         } catch (LabelSyntaxException e) {
           throw new TargetParsingException(
-              "Invalid package name '" + packagePart + "': " + e.getMessage());
+              "Invalid package name '" + packagePart + "': " + e.getMessage(),
+              TargetPatterns.Code.LABEL_SYNTAX_ERROR);
         }
         return new TargetsInPackage(originalPattern, relativeDirectory, packageIdentifier,
             targetPart, wasOriginallyAbsolute, true, true);
@@ -869,7 +882,8 @@
           packageIdentifier = PackageIdentifier.parse(repository.getName() + "//" + packagePart);
         } catch (LabelSyntaxException e) {
           throw new TargetParsingException(
-              "Invalid package name '" + packagePart + "': " + e.getMessage());
+              "Invalid package name '" + packagePart + "': " + e.getMessage(),
+              TargetPatterns.Code.LABEL_SYNTAX_ERROR);
         }
         return new TargetsInPackage(originalPattern, relativeDirectory, packageIdentifier,
             targetPart, wasOriginallyAbsolute, false, true);
@@ -885,7 +899,7 @@
                   repository, PathFragment.create(packageAndTarget.getPackageName()));
         } catch (BadLabelException e) {
           String error = "invalid target format '" + originalPattern + "': " + e.getMessage();
-          throw new TargetParsingException(error);
+          throw new TargetParsingException(error, TargetPatterns.Code.TARGET_FORMAT_INVALID);
         }
         return new SingleTarget(fullLabel, packageIdentifier, originalPattern, relativeDirectory);
       }
@@ -904,7 +918,8 @@
         PackageIdentifier.parse("//" + packageName);
       } catch (LabelSyntaxException e) {
         throw new TargetParsingException(
-            "Bad target pattern '" + originalPattern + "': " + e.getMessage());
+            "Bad target pattern '" + originalPattern + "': " + e.getMessage(),
+            TargetPatterns.Code.LABEL_SYNTAX_ERROR);
       }
       return new InterpretPathAsTarget(pattern, originalPattern, relativeDirectory);
     }
@@ -953,9 +968,12 @@
     try {
       return Label.parseAbsolute(label, ImmutableMap.of());
     } catch (LabelSyntaxException e) {
-      throw new TargetParsingException("invalid target format: '"
-          + StringUtilities.sanitizeControlChars(label) + "'; "
-          + StringUtilities.sanitizeControlChars(e.getMessage()));
+      throw new TargetParsingException(
+          "invalid target format: '"
+              + StringUtilities.sanitizeControlChars(label)
+              + "'; "
+              + StringUtilities.sanitizeControlChars(e.getMessage()),
+          TargetPatterns.Code.TARGET_FORMAT_INVALID);
     }
   }