Refactor ErrorInfo creation to share single constructor

Single constructor allows us to enforce/document high-level constraints
in a single place. Move previous constructors to static methods which do
their custom transformations but ultimately funnel their final calculated
fields through the common constructor.

Also added some tests to demonstrate expected behavior of static methods.

--
MOS_MIGRATED_REVID=104142909
diff --git a/src/main/java/com/google/devtools/build/skyframe/ErrorInfo.java b/src/main/java/com/google/devtools/build/skyframe/ErrorInfo.java
index 210b8ed..67c8da9 100644
--- a/src/main/java/com/google/devtools/build/skyframe/ErrorInfo.java
+++ b/src/main/java/com/google/devtools/build/skyframe/ErrorInfo.java
@@ -15,6 +15,7 @@
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
 import com.google.devtools.build.lib.collect.nestedset.NestedSet;
 import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
 import com.google.devtools.build.lib.collect.nestedset.Order;
@@ -32,47 +33,37 @@
  * <p>This is intended only for use in alternative {@code MemoizingEvaluator} implementations.
  */
 public class ErrorInfo implements Serializable {
-  /** The set of descendants of this value that failed to build. */
-  // Non-final only to allow for serialization.
-  private transient NestedSet<SkyKey> rootCauses;
 
-  /**
-   * An exception thrown upon a value's failure to build. The exception is used for reporting, and
-   * thus may ultimately be rethrown by the caller. As well, during a --nokeep_going evaluation, if
-   * an error value is encountered from an earlier --keep_going build, the exception to be thrown is
-   * taken from here.
-   */
-  @Nullable private final Exception exception;
-  private final SkyKey rootCauseOfException;
-
-  private final Iterable<CycleInfo> cycles;
-
-  private final boolean isTransient;
-  private final boolean isCatastrophic;
-
-  public ErrorInfo(ReifiedSkyFunctionException builderException) {
-    this.rootCauseOfException = builderException.getRootCauseSkyKey();
-    this.rootCauses = NestedSetBuilder.create(Order.STABLE_ORDER, rootCauseOfException);
-    this.exception = Preconditions.checkNotNull(builderException.getCause(), builderException);
-    this.cycles = ImmutableList.of();
-    this.isTransient = builderException.isTransient();
-    this.isCatastrophic = builderException.isCatastrophic();
+  /** Create an ErrorInfo from a {@link ReifiedSkyFunctionException}. */
+  public static ErrorInfo fromException(ReifiedSkyFunctionException skyFunctionException) {
+    SkyKey rootCauseSkyKey = skyFunctionException.getRootCauseSkyKey();
+    Exception rootCauseException = skyFunctionException.getCause();
+    return new ErrorInfo(
+        NestedSetBuilder.create(Order.STABLE_ORDER, rootCauseSkyKey),
+        Preconditions.checkNotNull(rootCauseException, "Cause null %s", rootCauseException),
+        rootCauseSkyKey,
+        /*cycles=*/ ImmutableList.<CycleInfo>of(),
+        skyFunctionException.isTransient(),
+        skyFunctionException.isCatastrophic());
   }
 
-  ErrorInfo(CycleInfo cycleInfo) {
-    this.rootCauses = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
-    this.exception = null;
-    this.rootCauseOfException = null;
-    this.cycles = ImmutableList.of(cycleInfo);
-    this.isTransient = false;
-    this.isCatastrophic = false;
+  /** Create an ErrorInfo from a {@link CycleInfo}. */
+  static ErrorInfo fromCycle(CycleInfo cycleInfo) {
+    return new ErrorInfo(
+        /*rootCauses=*/ NestedSetBuilder.<SkyKey>emptySet(Order.STABLE_ORDER),
+        /*exception=*/ null,
+        /*rootCauseOfException=*/ null,
+        ImmutableList.of(cycleInfo),
+        /*isTransient=*/ false,
+        /*isCatostrophic=*/ false);
   }
 
-  public ErrorInfo(SkyKey currentValue, Collection<ErrorInfo> childErrors) {
-    Preconditions.checkNotNull(currentValue);
-    Preconditions.checkState(!childErrors.isEmpty(),
-        "Error value %s with no exception must depend on another error value", currentValue);
-    NestedSetBuilder<SkyKey> builder = NestedSetBuilder.stableOrder();
+  /** Create an ErrorInfo from a collection of existing errors. */
+  public static ErrorInfo fromChildErrors(SkyKey currentValue, Collection<ErrorInfo> childErrors) {
+    Preconditions.checkNotNull(currentValue, "currentValue must not be null");
+    Preconditions.checkState(!childErrors.isEmpty(), "childErrors may not be empty");
+
+    NestedSetBuilder<SkyKey> rootCausesBuilder = NestedSetBuilder.stableOrder();
     ImmutableList.Builder<CycleInfo> cycleBuilder = ImmutableList.builder();
     Exception firstException = null;
     SkyKey firstChildKey = null;
@@ -83,18 +74,48 @@
         firstException = child.getException();
         firstChildKey = child.getRootCauseOfException();
       }
-      builder.addTransitive(child.rootCauses);
+      rootCausesBuilder.addTransitive(child.rootCauses);
       cycleBuilder.addAll(CycleInfo.prepareCycles(currentValue, child.cycles));
       isCatastrophic |= child.isCatastrophic();
     }
-    this.rootCauses = builder.build();
-    this.exception = firstException;
-    this.rootCauseOfException = firstChildKey;
-    this.cycles = cycleBuilder.build();
-    // Parent errors should not be transient -- we depend on the child's transience, if any, to
-    // force re-evaluation if necessary.
-    this.isTransient = false;
-    this.isCatastrophic = isCatastrophic;
+
+    return new ErrorInfo(
+        rootCausesBuilder.build(),
+        firstException,
+        firstChildKey,
+        cycleBuilder.build(),
+        // Parent errors should not be transient -- we depend on the child's transience, if any, to
+        // force re-evaluation if necessary.
+        /*isTransient=*/ false,
+        isCatastrophic);
+  }
+
+  // Non-final only to allow for serialization.
+  private transient NestedSet<SkyKey> rootCauses;
+
+  @Nullable private final Exception exception;
+  private final SkyKey rootCauseOfException;
+
+  private final ImmutableList<CycleInfo> cycles;
+
+  private final boolean isTransient;
+  private final boolean isCatastrophic;
+
+  public ErrorInfo(NestedSet<SkyKey> rootCauses, @Nullable Exception exception,
+      SkyKey rootCauseOfException, ImmutableList<CycleInfo> cycles, boolean isTransient,
+      boolean isCatostrophic) {
+    Preconditions.checkState(exception != null || !Iterables.isEmpty(cycles),
+        "At least one of exception and cycles must be non-null/empty, respectively");
+    Preconditions.checkState((exception == null) == (rootCauseOfException == null),
+        "exception and rootCauseOfException must both be null or non-null, got %s  %s",
+        exception, rootCauseOfException);
+
+    this.rootCauses = rootCauses;
+    this.exception = exception;
+    this.rootCauseOfException = rootCauseOfException;
+    this.cycles = cycles;
+    this.isTransient = isTransient;
+    this.isCatastrophic = isCatostrophic;
   }
 
   @Override
@@ -116,6 +137,10 @@
   /**
    * The exception thrown when building a value. May be null if value's only error is depending
    * on a cycle.
+   *
+   * <p>The exception is used for reporting and thus may ultimately be rethrown by the caller.
+   * As well, during a --nokeep_going evaluation, if an error value is encountered from an earlier
+   * --keep_going build, the exception to be thrown is taken from here.
    */
   @Nullable public Exception getException() {
     return exception;