Search in sources :

Example 11 with MessagingException

use of org.mule.runtime.core.internal.exception.MessagingException in project mule by mulesoft.

the class MessagingExceptionResolver method resolve.

/**
 * Resolves a new {@link MessagingException} with the real cause of the problem based on the content of an Incoming
 * {@link MessagingException} with a chain of causes inside it and the current event that the exception is carrying.
 * <p>
 * This method will pick the FIRST cause exception that has a mule or extension KNOWN error as the real cause, if there is not
 * an exception in the causes that match with an Known error type then this method will try to find the error that the current
 * {@link Event} is carrying.
 * <p>
 * When there are multiple exceptions that contains the same root error type, then this method will wrap the one that has
 * highest position in the causes list
 *
 * @return a {@link MessagingException} with the proper {@link Error} associated to it's {@link CoreEvent}
 */
public MessagingException resolve(final MessagingException me, MuleContext context) {
    ErrorTypeLocator locator = ((PrivilegedMuleContext) context).getErrorTypeLocator();
    Optional<Pair<Throwable, ErrorType>> rootCause = findRoot(component, me, locator);
    if (!rootCause.isPresent()) {
        return updateCurrent(me, component, context);
    }
    Throwable root = rootCause.get().getFirst();
    ErrorType rootErrorType = rootCause.get().getSecond();
    Component failingComponent = getFailingProcessor(me, root).orElse(component);
    ErrorType errorType = getErrorMappings(component).stream().filter(m -> m.match(rootErrorType)).findFirst().map(ErrorMapping::getTarget).orElse(rootErrorType);
    Error error = ErrorBuilder.builder(getMessagingExceptionCause(root)).errorType(errorType).build();
    CoreEvent event = CoreEvent.builder(me.getEvent()).error(error).build();
    MessagingException result;
    if (root instanceof MessagingException) {
        ((MessagingException) root).setProcessedEvent(event);
        result = ((MessagingException) root);
    } else {
        result = me instanceof FlowExecutionException ? new FlowExecutionException(event, root, failingComponent) : new MessagingException(event, root, failingComponent);
    }
    if (me.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
        result.addInfo(INFO_ALREADY_LOGGED_KEY, me.getInfo().get(INFO_ALREADY_LOGGED_KEY));
    }
    return enrich(result, failingComponent, event, context);
}
Also used : EnrichedNotificationInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) ExceptionHelper.getExceptionsAsList(org.mule.runtime.api.exception.ExceptionHelper.getExceptionsAsList) Event(org.mule.runtime.api.event.Event) MuleContext(org.mule.runtime.core.api.MuleContext) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Component(org.mule.runtime.api.component.Component) ExceptionUtils.getMessagingExceptionCause(org.mule.runtime.core.api.util.ExceptionUtils.getMessagingExceptionCause) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CRITICAL_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CRITICAL_IDENTIFIER) Pair(org.mule.runtime.api.util.Pair) LinkedList(java.util.LinkedList) Error(org.mule.runtime.api.message.Error) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) INFO_ALREADY_LOGGED_KEY(org.mule.runtime.api.exception.MuleException.INFO_ALREADY_LOGGED_KEY) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EnrichedNotificationInfo.createInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo.createInfo) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) InternalExceptionUtils.getErrorMappings(org.mule.runtime.core.internal.util.InternalExceptionUtils.getErrorMappings) Reference(org.mule.runtime.api.util.Reference) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Optional(java.util.Optional) CORE_NAMESPACE_NAME(org.mule.runtime.core.api.exception.Errors.CORE_NAMESPACE_NAME) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Error(org.mule.runtime.api.message.Error) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) Component(org.mule.runtime.api.component.Component) Pair(org.mule.runtime.api.util.Pair)

Example 12 with MessagingException

use of org.mule.runtime.core.internal.exception.MessagingException in project mule by mulesoft.

the class MessagingExceptionResolver method updateCurrent.

private MessagingException updateCurrent(MessagingException me, Component processor, MuleContext context) {
    CoreEvent errorEvent = createErrorEvent(me.getEvent(), processor, me, ((PrivilegedMuleContext) context).getErrorTypeLocator());
    Component failingProcessor = me.getFailingComponent() != null ? me.getFailingComponent() : processor;
    MessagingException updated = me instanceof FlowExecutionException ? new FlowExecutionException(errorEvent, me.getCause(), failingProcessor) : new MessagingException(me.getI18nMessage(), errorEvent, me.getCause(), failingProcessor);
    return enrich(updated, failingProcessor, errorEvent, context);
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Component(org.mule.runtime.api.component.Component)

Example 13 with MessagingException

use of org.mule.runtime.core.internal.exception.MessagingException in project mule by mulesoft.

the class AbstractExecutableComponent method execute.

@Override
public final CompletableFuture<ExecutionResult> execute(InputEvent inputEvent) {
    CompletableFuture completableFuture = new CompletableFuture();
    CoreEvent.Builder builder = CoreEvent.builder(createEventContext(of(completableFuture)));
    CoreEvent event = builder.message(inputEvent.getMessage()).error(inputEvent.getError().orElse(null)).variables(inputEvent.getVariables()).build();
    return from(MessageProcessors.process(event, getExecutableFunction())).onErrorMap(throwable -> {
        MessagingException messagingException = (MessagingException) throwable;
        CoreEvent messagingExceptionEvent = messagingException.getEvent();
        return new ComponentExecutionException(messagingExceptionEvent.getError().get().getCause(), messagingExceptionEvent);
    }).<ExecutionResult>map(result -> new ExecutionResultImplementation(result, completableFuture)).toFuture();
}
Also used : Optional.of(java.util.Optional.of) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) Event(org.mule.runtime.api.event.Event) ExecutionResult(org.mule.runtime.api.component.execution.ExecutionResult) Inject(javax.inject.Inject) MuleContext(org.mule.runtime.core.api.MuleContext) Mono.from(reactor.core.publisher.Mono.from) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EventContext(org.mule.runtime.api.event.EventContext) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ReactiveProcessor(org.mule.runtime.core.api.processor.ReactiveProcessor) InputEvent(org.mule.runtime.api.component.execution.InputEvent) String.format(java.lang.String.format) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MessageProcessors(org.mule.runtime.core.privileged.processor.MessageProcessors) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) Optional(java.util.Optional) DefaultEventContext.child(org.mule.runtime.core.internal.event.DefaultEventContext.child) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException)

Example 14 with MessagingException

use of org.mule.runtime.core.internal.exception.MessagingException in project mule by mulesoft.

the class IdempotentRedeliveryPolicy method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    Optional<Exception> exceptionSeen = empty();
    String messageId = null;
    try {
        messageId = getIdForEvent(event);
    } catch (ExpressionRuntimeException e) {
        logger.warn("The message cannot be processed because the digest could not be generated. Either make the payload serializable or use an expression.");
        return null;
    } catch (Exception ex) {
        exceptionSeen = of(ex);
    }
    Lock lock = lockFactory.createLock(idrId + "-" + messageId);
    lock.lock();
    try {
        RedeliveryCounter counter = findCounter(messageId);
        if (exceptionSeen.isPresent()) {
            throw new MessageRedeliveredException(messageId, counter.counter.get(), maxRedeliveryCount, exceptionSeen.get());
        } else if (counter != null && counter.counter.get() > maxRedeliveryCount) {
            throw new MessageRedeliveredException(messageId, counter.errors, counter.counter.get(), maxRedeliveryCount);
        }
        try {
            CoreEvent returnEvent = processNext(CoreEvent.builder(DefaultEventContext.child((BaseEventContext) event.getContext(), empty()), event).build());
            counter = findCounter(messageId);
            if (counter != null) {
                resetCounter(messageId);
            }
            return returnEvent;
        } catch (Exception ex) {
            if (ex instanceof MessagingException) {
                incrementCounter(messageId, (MessagingException) ex);
                throw ex;
            } else {
                MessagingException me = createMessagingException(event, ex, this);
                incrementCounter(messageId, me);
                throw ex;
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) Lock(java.util.concurrent.locks.Lock)

Example 15 with MessagingException

use of org.mule.runtime.core.internal.exception.MessagingException in project mule by mulesoft.

the class MessagingExceptionTestCase method causedExactlyByWithNonMuleCause.

@Test
public void causedExactlyByWithNonMuleCause() {
    IOException causeException = new IOException("");
    MessagingException exception = new MessagingException(createStaticMessage(""), testEvent, causeException);
    assertThat(exception.causedExactlyBy(IOException.class), is(true));
    assertThat(exception.causedExactlyBy(MessagingException.class), is(true));
    assertThat(exception.causedExactlyBy(Exception.class), is(false));
    assertThat(exception.causedExactlyBy(NullPointerException.class), is(false));
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) IOException(java.io.IOException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) SocketException(java.net.SocketException) MuleException(org.mule.runtime.api.exception.MuleException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

MessagingException (org.mule.runtime.core.internal.exception.MessagingException)69 Test (org.junit.Test)42 SmallTest (org.mule.tck.size.SmallTest)37 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)26 MuleException (org.mule.runtime.api.exception.MuleException)17 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)13 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)11 Optional (java.util.Optional)9 Error (org.mule.runtime.api.message.Error)9 Message (org.mule.runtime.api.message.Message)9 IOException (java.io.IOException)8 Processor (org.mule.runtime.core.api.processor.Processor)8 Publisher (org.reactivestreams.Publisher)8 ConnectException (java.net.ConnectException)7 Component (org.mule.runtime.api.component.Component)7 ConnectionException (org.mule.runtime.api.connection.ConnectionException)7 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)7 SocketException (java.net.SocketException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 MuleFatalException (org.mule.runtime.api.exception.MuleFatalException)6