Search in sources :

Example 6 with Message

use of org.elasticsearch.common.inject.spi.Message in project elasticsearch by elastic.

the class Errors method getOnlyCause.

/**
     * Returns the cause throwable if there is exactly one cause in {@code messages}. If there are
     * zero or multiple messages with causes, null is returned.
     */
public static Throwable getOnlyCause(Collection<Message> messages) {
    Throwable onlyCause = null;
    for (Message message : messages) {
        Throwable messageCause = message.getCause();
        if (messageCause == null) {
            continue;
        }
        if (onlyCause != null) {
            return null;
        }
        onlyCause = messageCause;
    }
    return onlyCause;
}
Also used : Message(org.elasticsearch.common.inject.spi.Message)

Example 7 with Message

use of org.elasticsearch.common.inject.spi.Message in project elasticsearch by elastic.

the class Errors method format.

/**
     * Returns the formatted message for an exception with the specified messages.
     */
public static String format(String heading, Collection<Message> errorMessages) {
    try (Formatter fmt = new Formatter(Locale.ROOT)) {
        fmt.format(heading).format(":%n%n");
        int index = 1;
        boolean displayCauses = getOnlyCause(errorMessages) == null;
        for (Message errorMessage : errorMessages) {
            fmt.format("%s) %s%n", index++, errorMessage.getMessage());
            List<Object> dependencies = errorMessage.getSources();
            for (int i = dependencies.size() - 1; i >= 0; i--) {
                Object source = dependencies.get(i);
                formatSource(fmt, source);
            }
            Throwable cause = errorMessage.getCause();
            if (displayCauses && cause != null) {
                StringWriter writer = new StringWriter();
                cause.printStackTrace(new PrintWriter(writer));
                fmt.format("Caused by: %s", writer.getBuffer());
            }
            fmt.format("%n");
        }
        if (errorMessages.size() == 1) {
            fmt.format("1 error");
        } else {
            fmt.format("%s errors", errorMessages.size());
        }
        return fmt.toString();
    }
}
Also used : Message(org.elasticsearch.common.inject.spi.Message) StringWriter(java.io.StringWriter) Formatter(java.util.Formatter) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) PrintWriter(java.io.PrintWriter)

Example 8 with Message

use of org.elasticsearch.common.inject.spi.Message in project elasticsearch by elastic.

the class Errors method addMessage.

private Errors addMessage(Throwable cause, String messageFormat, Object... arguments) {
    String message = format(messageFormat, arguments);
    addMessage(new Message(getSources(), message, cause));
    return this;
}
Also used : Message(org.elasticsearch.common.inject.spi.Message)

Example 9 with Message

use of org.elasticsearch.common.inject.spi.Message in project crate by crate.

the class Errors method format.

/**
 * Returns the formatted message for an exception with the specified messages.
 */
public static String format(String heading, Collection<Message> errorMessages) {
    try (Formatter fmt = new Formatter(Locale.ROOT)) {
        fmt.format(heading).format(":%n%n");
        int index = 1;
        boolean displayCauses = getOnlyCause(errorMessages) == null;
        for (Message errorMessage : errorMessages) {
            fmt.format("%s) %s%n", index++, errorMessage.getMessage());
            List<Object> dependencies = errorMessage.getSources();
            for (int i = dependencies.size() - 1; i >= 0; i--) {
                Object source = dependencies.get(i);
                formatSource(fmt, source);
            }
            Throwable cause = errorMessage.getCause();
            if (displayCauses && cause != null) {
                StringWriter writer = new StringWriter();
                cause.printStackTrace(new PrintWriter(writer));
                fmt.format("Caused by: %s", writer.getBuffer());
            }
            fmt.format("%n");
        }
        if (errorMessages.size() == 1) {
            fmt.format("1 error");
        } else {
            fmt.format("%s errors", errorMessages.size());
        }
        return fmt.toString();
    }
}
Also used : Message(org.elasticsearch.common.inject.spi.Message) StringWriter(java.io.StringWriter) Formatter(java.util.Formatter) InjectionPoint(org.elasticsearch.common.inject.spi.InjectionPoint) PrintWriter(java.io.PrintWriter)

Example 10 with Message

use of org.elasticsearch.common.inject.spi.Message in project crate by crate.

the class Errors method getOnlyCause.

/**
 * Returns the cause throwable if there is exactly one cause in {@code messages}. If there are
 * zero or multiple messages with causes, null is returned.
 */
public static Throwable getOnlyCause(Collection<Message> messages) {
    Throwable onlyCause = null;
    for (Message message : messages) {
        Throwable messageCause = message.getCause();
        if (messageCause == null) {
            continue;
        }
        if (onlyCause != null) {
            return null;
        }
        onlyCause = messageCause;
    }
    return onlyCause;
}
Also used : Message(org.elasticsearch.common.inject.spi.Message)

Aggregations

Message (org.elasticsearch.common.inject.spi.Message)10 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2 HashSet (java.util.HashSet)2 CreationException (org.elasticsearch.common.inject.CreationException)2 Provider (org.elasticsearch.common.inject.Provider)2 TypeLiteral (org.elasticsearch.common.inject.TypeLiteral)2 Dependency (org.elasticsearch.common.inject.spi.Dependency)2 InjectionPoint (org.elasticsearch.common.inject.spi.InjectionPoint)2 RepositoryException (org.elasticsearch.repositories.RepositoryException)2 Test (org.junit.Test)2 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 CrateUnitTest (io.crate.test.integration.CrateUnitTest)1