All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.spi.ErrorDetail Maven / Gradle / Ivy

The newest version!
package com.google.inject.spi;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableList;
import com.google.inject.internal.Messages;
import java.io.Serializable;
import java.util.Formatter;
import java.util.List;
import java.util.Optional;

/**
 * Details about a single Guice error and supports formatting itself in the context of other Guice
 * errors.
 *
 * 

WARNING: The class and its APIs are still experimental and subject to change. * * @since 5.0 */ public abstract class ErrorDetail> implements Serializable { private final String message; private final ImmutableList sources; private final Throwable cause; protected ErrorDetail(String message, List sources, Throwable cause) { this.message = message; this.sources = ImmutableList.copyOf(sources); this.cause = cause; } /** * Returns true if this error can be merged with the {@code otherError} and formatted together. * *

By default this return false and implementations that support merging with other errors * should override this method. */ public boolean isMergeable(ErrorDetail otherError) { return false; } /** * Formats this error along with other errors that are mergeable with this error. * *

{@code mergeableErrors} is a list that contains all other errors that are reported in the * same exception that are considered to be mergable with this error base on result of calling * {@link #isMergeable}. The list will be empty if non of the other errors are mergable with this * error. * *

Formatted error has the following structure: * *

    *
  • Summary of the error *
  • Details about the error such as the source of the error *
  • Hints for fixing the error if available *
  • Link to the documentation on this error in greater detail *
* * @param index index for this error * @param mergeableErrors list of errors that are mergeable with this error * @param formatter for printing the error message */ public final void format(int index, List> mergeableErrors, Formatter formatter) { String id = getErrorIdentifier().map(s -> "[" + Messages.redBold(s) + "]: ").orElse(""); formatter.format("%s) %s%s\n", index, id, getMessage()); formatDetail(mergeableErrors, formatter); // TODO(b/151482394): Output potiential fixes for the error Optional learnMoreLink = getLearnMoreLink(); if (learnMoreLink.isPresent()) { formatter.format("\n%s\n", Messages.bold("Learn more:")); formatter.format(" %s\n", Messages.underline(learnMoreLink.get())); } } /** * Formats the detail of this error message along with other errors that are mergeable with this * error. This is called from {@link #format}. * *

{@code mergeableErrors} is a list that contains all other errors that are reported in the * same exception that are considered to be mergable with this error base on result of calling * {@link #isMergeable}. The list will be empty if non of the other errors are mergable with this * error. * * @param mergeableErrors list of errors that are mergeable with this error * @param formatter for printing the error message */ protected abstract void formatDetail(List> mergeableErrors, Formatter formatter); /** * Returns an optional link to additional documentation about this error to be included in the * formatted error message. */ protected Optional getLearnMoreLink() { return Optional.empty(); } /** Returns an optional string identifier for this error. */ protected Optional getErrorIdentifier() { return Optional.empty(); } public String getMessage() { return message; } public List getSources() { return sources; } public Throwable getCause() { return cause; } @Override public int hashCode() { return Objects.hashCode(message, cause, sources); } @Override public boolean equals(Object o) { if (!(o instanceof ErrorDetail)) { return false; } ErrorDetail e = (ErrorDetail) o; return message.equals(e.message) && Objects.equal(cause, e.cause) && sources.equals(e.sources); } /** Returns a new instance of the same {@link ErrorDetail} with updated sources. */ public abstract SelfT withSources(List newSources); }