Search in sources :

Example 21 with Message

use of com.google.inject.spi.Message in project roboguice by roboguice.

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) {
    Formatter fmt = new Formatter().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(com.google.inject.spi.Message) StringWriter(java.io.StringWriter) Formatter(java.util.Formatter) InjectionPoint(com.google.inject.spi.InjectionPoint) PrintWriter(java.io.PrintWriter)

Example 22 with Message

use of com.google.inject.spi.Message in project roboguice by roboguice.

the class BinderTest method testUserReportedErrorsAreAlsoLogged.

public void testUserReportedErrorsAreAlsoLogged() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                addError(new Message("Whoops!", new IllegalArgumentException()));
            }
        });
        fail();
    } catch (CreationException expected) {
    }
    LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
    assertContains(logRecord.getMessage(), "An exception was caught and reported. Message: java.lang.IllegalArgumentException");
}
Also used : Message(com.google.inject.spi.Message) LogRecord(java.util.logging.LogRecord) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 23 with Message

use of com.google.inject.spi.Message in project roboguice by roboguice.

the class FactoryProvider2 method invoke.

/**
 * When a factory method is invoked, we create a child injector that binds all parameters, then
 * use that to get an instance of the return type.
 */
public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
    if (method.getDeclaringClass().equals(Object.class)) {
        if ("equals".equals(method.getName())) {
            return proxy == args[0];
        } else if ("hashCode".equals(method.getName())) {
            return System.identityHashCode(proxy);
        } else {
            return method.invoke(this, args);
        }
    }
    AssistData data = assistDataByMethod.get(method);
    Provider<?> provider;
    if (data.cachedBinding != null) {
        // Try to get optimized form...
        provider = data.cachedBinding.getProvider();
    } else {
        provider = getBindingFromNewInjector(method, args, data).getProvider();
    }
    try {
        int p = 0;
        for (ThreadLocalProvider tlp : data.providers) {
            tlp.set(args[p++]);
        }
        return provider.get();
    } catch (ProvisionException e) {
        // if this is an exception declared by the factory method, throw it as-is
        if (e.getErrorMessages().size() == 1) {
            Message onlyError = getOnlyElement(e.getErrorMessages());
            Throwable cause = onlyError.getCause();
            if (cause != null && canRethrow(method, cause)) {
                throw cause;
            }
        }
        throw e;
    } finally {
        for (ThreadLocalProvider tlp : data.providers) {
            tlp.remove();
        }
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Message(com.google.inject.spi.Message) InjectionPoint(com.google.inject.spi.InjectionPoint)

Example 24 with Message

use of com.google.inject.spi.Message in project graylog2-server by Graylog2.

the class Server method annotateInjectorExceptions.

@Override
protected void annotateInjectorExceptions(Collection<Message> messages) {
    super.annotateInjectorExceptions(messages);
    for (Message message : messages) {
        if (message.getCause() instanceof MongoException) {
            MongoException e = (MongoException) message.getCause();
            LOG.error(UI.wallString("Unable to connect to MongoDB. Is it running and the configuration correct?\n" + "Details: " + e.getMessage()));
            System.exit(-1);
        }
    }
}
Also used : MongoException(com.mongodb.MongoException) Message(com.google.inject.spi.Message)

Example 25 with Message

use of com.google.inject.spi.Message in project graylog2-server by Graylog2.

the class CmdLineTool method annotateInjectorExceptions.

protected void annotateInjectorExceptions(Collection<Message> messages) {
    for (Message message : messages) {
        // noinspection ThrowableResultOfMethodCallIgnored
        final Throwable rootCause = ExceptionUtils.getRootCause(message.getCause());
        if (rootCause instanceof NodeIdPersistenceException) {
            LOG.error(UI.wallString("Unable to read or persist your NodeId file. This means your node id file (" + configuration.getNodeIdFile() + ") is not readable or writable by the current user. The following exception might give more information: " + message));
            System.exit(-1);
        } else if (rootCause instanceof AccessDeniedException) {
            LOG.error(UI.wallString("Unable to access file " + rootCause.getMessage()));
            System.exit(-2);
        } else if (rootCause instanceof UnsupportedSearchException) {
            final SearchVersion search = ((UnsupportedSearchException) rootCause).getSearchMajorVersion();
            LOG.error(UI.wallString("Unsupported search version: " + search, DocsHelper.PAGE_ES_VERSIONS.toString()));
            System.exit(-3);
        } else if (rootCause instanceof ElasticsearchProbeException) {
            LOG.error(UI.wallString(rootCause.getMessage(), DocsHelper.PAGE_ES_CONFIGURATION.toString()));
            System.exit(-4);
        } else {
            // other guice error, still print the raw messages
            // TODO this could potentially print duplicate messages depending on what a subclass does...
            LOG.error("Guice error (more detail on log level debug): {}", message.getMessage());
            if (rootCause != null) {
                LOG.debug("Stacktrace:", rootCause);
            }
        }
    }
}
Also used : ElasticsearchProbeException(org.graylog2.storage.versionprobe.ElasticsearchProbeException) AccessDeniedException(java.nio.file.AccessDeniedException) Message(com.google.inject.spi.Message) UnsupportedSearchException(org.graylog2.storage.UnsupportedSearchException) SearchVersion(org.graylog2.storage.SearchVersion) NodeIdPersistenceException(org.graylog2.plugin.system.NodeIdPersistenceException)

Aggregations

Message (com.google.inject.spi.Message)49 ProvisionException (com.google.inject.ProvisionException)9 CreationException (com.google.inject.CreationException)7 ArrayList (java.util.ArrayList)7 AbstractModule (com.google.inject.AbstractModule)6 Provider (com.google.inject.Provider)5 TypeLiteral (com.google.inject.TypeLiteral)5 Injector (com.google.inject.Injector)4 Module (com.google.inject.Module)4 Errors (com.google.inject.internal.Errors)4 InjectionPoint (com.google.inject.spi.InjectionPoint)4 DisabledMetricMaker (com.google.gerrit.metrics.DisabledMetricMaker)3 GerritServerConfigModule (com.google.gerrit.server.config.GerritServerConfigModule)3 SitePath (com.google.gerrit.server.config.SitePath)3 Test (org.junit.Test)3 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)2 Function (com.google.common.base.Function)2 ImmutableList (com.google.common.collect.ImmutableList)2