Search in sources :

Example 26 with MessagingException

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

the class DefaultMessageProcessorChainTestCase method testErrorNotificationsMessagingException.

@Test
public void testErrorNotificationsMessagingException() throws Exception {
    List<MessageProcessorNotification> notificationList = new ArrayList<>();
    setupMessageProcessorNotificationListener(notificationList);
    DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder();
    final CoreEvent messagingExceptionEvent = getTestEventUsingFlow("other");
    final MessagingException messagingException = new MessagingException(messagingExceptionEvent, illegalStateException);
    builder.chain(new ExceptionThrowingMessageProcessor(messagingException));
    final CoreEvent inEvent = getTestEventUsingFlow("0");
    try {
        process(builder.build(), inEvent);
    } catch (Throwable t) {
        assertThat(t, instanceOf(IllegalStateException.class));
        assertThat(notificationList, hasSize(2));
        MessageProcessorNotification preNotification = notificationList.get(0);
        MessageProcessorNotification postNotification = notificationList.get(1);
        assertPreNotification(inEvent, preNotification);
        assertThat(postNotification.getAction().getActionId(), equalTo(MESSAGE_PROCESSOR_POST_INVOKE));
        // Existing MessagingException.event is updated to include error
        assertThat(postNotification.getEvent(), not(messagingExceptionEvent));
        assertPostErrorNotification(inEvent, postNotification);
    }
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ArrayList(java.util.ArrayList) MessageProcessorNotification(org.mule.runtime.api.notification.MessageProcessorNotification) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 27 with MessagingException

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

the class FlowProcessingPhase method runPhase.

@Override
public void runPhase(final FlowProcessingPhaseTemplate flowProcessingPhaseTemplate, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    Runnable flowExecutionWork = () -> {
        try {
            FlowConstruct flowConstruct = registry.<FlowConstruct>lookupByName(messageProcessContext.getMessageSource().getRootContainerLocation().toString()).get();
            try {
                final AtomicReference exceptionThrownDuringFlowProcessing = new AtomicReference();
                TransactionalExecutionTemplate<CoreEvent> transactionTemplate = createTransactionalExecutionTemplate(muleContext, messageProcessContext.getTransactionConfig().orElse(new MuleTransactionConfig()));
                CoreEvent response = transactionTemplate.execute(() -> {
                    try {
                        Object message = flowProcessingPhaseTemplate.getOriginalMessage();
                        if (message == null) {
                            return null;
                        }
                        CoreEvent muleEvent = flowProcessingPhaseTemplate.getEvent();
                        muleEvent = flowProcessingPhaseTemplate.beforeRouteEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.routeEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.afterRouteEvent(muleEvent);
                        sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, muleEvent, flowProcessingPhaseTemplate);
                        return muleEvent;
                    } catch (Exception e) {
                        exceptionThrownDuringFlowProcessing.set(e);
                        throw e;
                    }
                });
                if (exceptionThrownDuringFlowProcessing.get() != null && !(exceptionThrownDuringFlowProcessing.get() instanceof ResponseDispatchException)) {
                    sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, response, flowProcessingPhaseTemplate);
                }
                flowProcessingPhaseTemplate.afterSuccessfulProcessingFlow(response);
            } catch (ResponseDispatchException e) {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            } catch (MessagingException e) {
                sendFailureResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, e, flowProcessingPhaseTemplate);
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            }
            phaseResultNotifier.phaseSuccessfully();
        } catch (Exception e) {
            MuleException me = new DefaultMuleException(e);
            try {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(me);
            } catch (MuleException e1) {
                logger.warn("Failure during exception processing in flow template: " + e.getMessage());
                if (logger.isDebugEnabled()) {
                    logger.debug("Failure during exception processing in flow template: ", e);
                }
            }
            phaseResultNotifier.phaseFailure(e);
        }
    };
    if (messageProcessContext.supportsAsynchronousProcessing()) {
        try {
            messageProcessContext.getFlowExecutionExecutor().execute(flowExecutionWork);
        } catch (SchedulerBusyException e) {
            phaseResultNotifier.phaseFailure(e);
        }
    } else {
        flowExecutionWork.run();
    }
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicReference(java.util.concurrent.atomic.AtomicReference) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) TransactionalExecutionTemplate.createTransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate.createTransactionalExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException)

Example 28 with MessagingException

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

the class ModuleFlowProcessingPhase method runPhase.

@Override
public void runPhase(final ModuleFlowProcessingPhaseTemplate template, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    try {
        final MessageSource messageSource = messageProcessContext.getMessageSource();
        final FlowConstruct flowConstruct = (FlowConstruct) componentLocator.find(messageSource.getRootContainerLocation()).get();
        final ComponentLocation sourceLocation = messageSource.getLocation();
        final Consumer<Either<MessagingException, CoreEvent>> terminateConsumer = getTerminateConsumer(messageSource, template);
        final CompletableFuture<Void> responseCompletion = new CompletableFuture<>();
        final CoreEvent templateEvent = createEvent(template, sourceLocation, responseCompletion, flowConstruct);
        try {
            FlowProcessor flowExecutionProcessor = new FlowProcessor(template, flowConstruct.getExceptionListener(), templateEvent);
            flowExecutionProcessor.setAnnotations(flowConstruct.getAnnotations());
            final SourcePolicy policy = policyManager.createSourcePolicyInstance(messageSource, templateEvent, flowExecutionProcessor, template);
            final PhaseContext phaseContext = new PhaseContext(template, messageProcessContext, phaseResultNotifier, terminateConsumer);
            just(templateEvent).doOnNext(onMessageReceived(template, messageProcessContext, flowConstruct)).flatMap(request -> from(policy.process(request))).flatMap(policyResult -> policyResult.reduce(policyFailure(phaseContext, flowConstruct, messageSource), policySuccess(phaseContext, flowConstruct, messageSource))).doOnSuccess(aVoid -> phaseResultNotifier.phaseSuccessfully()).doOnError(onFailure(flowConstruct, messageSource, phaseResultNotifier, terminateConsumer)).doAfterTerminate(() -> responseCompletion.complete(null)).subscribe();
        } catch (Exception e) {
            from(template.sendFailureResponseToClient(new MessagingExceptionResolver(messageProcessContext.getMessageSource()).resolve(new MessagingException(templateEvent, e), muleContext), template.getFailedExecutionResponseParametersFunction().apply(templateEvent))).doOnTerminate(() -> phaseResultNotifier.phaseFailure(e)).subscribe();
        }
    } catch (Exception t) {
        phaseResultNotifier.phaseFailure(t);
    }
}
Also used : Message(org.mule.runtime.api.message.Message) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) CoreEvent.builder(org.mule.runtime.core.api.event.CoreEvent.builder) FunctionalUtils.safely(org.mule.runtime.core.internal.util.FunctionalUtils.safely) Map(java.util.Map) SOURCE_ERROR_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_GENERATE) Mono.just(reactor.core.publisher.Mono.just) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) PolicyManager(org.mule.runtime.core.internal.policy.PolicyManager) SOURCE_ERROR_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_ERROR_RESPONSE_SEND) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) Collection(java.util.Collection) NullExceptionHandler(org.mule.runtime.core.api.exception.NullExceptionHandler) FlowExceptionHandler(org.mule.runtime.core.api.exception.FlowExceptionHandler) Message.of(org.mule.runtime.api.message.Message.of) Either.left(org.mule.runtime.core.api.functional.Either.left) ErrorBuilder.builder(org.mule.runtime.core.internal.message.ErrorBuilder.builder) Either.right(org.mule.runtime.core.api.functional.Either.right) MessageProcessors.processWithChildContext(org.mule.runtime.core.privileged.processor.MessageProcessors.processWithChildContext) MESSAGE_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RESPONSE) MessageUtils.toMessage(org.mule.runtime.core.internal.util.message.MessageUtils.toMessage) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) SOURCE_RESPONSE_SEND(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_SEND) SourcePolicySuccessResult(org.mule.runtime.core.internal.policy.SourcePolicySuccessResult) MessageSource(org.mule.runtime.core.api.source.MessageSource) MessageProcessContext(org.mule.runtime.core.privileged.execution.MessageProcessContext) CompletableFuture(java.util.concurrent.CompletableFuture) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) Processor(org.mule.runtime.core.api.processor.Processor) Function(java.util.function.Function) MessageUtils.toMessageCollection(org.mule.runtime.core.internal.util.message.MessageUtils.toMessageCollection) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) MuleException(org.mule.runtime.api.exception.MuleException) SourcePolicyFailureResult(org.mule.runtime.core.internal.policy.SourcePolicyFailureResult) Mono.from(reactor.core.publisher.Mono.from) SOURCE_RESPONSE_GENERATE(org.mule.runtime.core.api.exception.Errors.ComponentIdentifiers.Handleable.SOURCE_RESPONSE_GENERATE) Mono.error(reactor.core.publisher.Mono.error) Mono.fromCallable(reactor.core.publisher.Mono.fromCallable) MESSAGE_RECEIVED(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_RECEIVED) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) MESSAGE_ERROR_RESPONSE(org.mule.runtime.api.notification.ConnectorMessageNotification.MESSAGE_ERROR_RESPONSE) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Publisher(org.reactivestreams.Publisher) EventContextFactory.create(org.mule.runtime.core.api.event.EventContextFactory.create) Mono(reactor.core.publisher.Mono) Initialisable(org.mule.runtime.api.lifecycle.Initialisable) Result(org.mule.runtime.extension.api.runtime.operation.Result) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Mono.when(reactor.core.publisher.Mono.when) TypedValue(org.mule.runtime.api.metadata.TypedValue) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) MessageProcessTemplate(org.mule.runtime.core.privileged.execution.MessageProcessTemplate) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) Builder(org.mule.runtime.core.internal.message.InternalEvent.Builder) ANY(org.mule.runtime.api.metadata.MediaType.ANY) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) InternalEvent(org.mule.runtime.core.internal.message.InternalEvent) SourcePolicy(org.mule.runtime.core.internal.policy.SourcePolicy) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) MessageSource(org.mule.runtime.core.api.source.MessageSource) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MuleException(org.mule.runtime.api.exception.MuleException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ComponentLocation(org.mule.runtime.api.component.location.ComponentLocation) CompletableFuture(java.util.concurrent.CompletableFuture) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Either(org.mule.runtime.core.api.functional.Either) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver)

Example 29 with MessagingException

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

the class BeginAndResolveTransactionInterceptor method execute.

@Override
public T execute(ExecutionCallback<T> callback, ExecutionContext executionContext) throws Exception {
    byte action = transactionConfig.getAction();
    int timeout = transactionConfig.getTimeout();
    boolean resolveStartedTransaction = false;
    Transaction tx = TransactionCoordination.getInstance().getTransaction();
    if (action == TransactionConfig.ACTION_ALWAYS_BEGIN || (action == TransactionConfig.ACTION_BEGIN_OR_JOIN && tx == null)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Beginning transaction");
        }
        executionContext.markTransactionStart();
        tx = transactionConfig.getFactory().beginTransaction(muleContext);
        // Timeout is a traversal attribute of all Transaction implementations.
        // Setting it up here for all of them rather than in every implementation.
        tx.setTimeout(timeout);
        resolveStartedTransaction = true;
        if (logger.isDebugEnabled()) {
            logger.debug("Transaction successfully started: " + tx);
        }
    }
    T result;
    try {
        result = next.execute(callback, executionContext);
        resolveTransactionIfRequired(resolveStartedTransaction);
        return result;
    } catch (MessagingException e) {
        if (processOnException) {
            resolveTransactionIfRequired(resolveStartedTransaction || mustResolveAnyTransaction);
        }
        throw e;
    }
}
Also used : Transaction(org.mule.runtime.core.api.transaction.Transaction) MessagingException(org.mule.runtime.core.internal.exception.MessagingException)

Example 30 with MessagingException

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

the class SourceErrorException method toMessagingException.

public MessagingException toMessagingException(Collection<ExceptionContextProvider> exceptionContextProviders, MessageSource messageSource) {
    MessagingException messagingException = new MessagingException(CoreEvent.builder(getEvent()).error(ErrorBuilder.builder(getCause()).errorType(getErrorType()).build()).build(), getCause());
    EnrichedNotificationInfo notificationInfo = createInfo(messagingException.getEvent(), messagingException, null);
    exceptionContextProviders.forEach(cp -> {
        cp.getContextInfo(notificationInfo, messageSource).forEach((k, v) -> messagingException.getInfo().putIfAbsent(k, v));
    });
    return messagingException;
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) EnrichedNotificationInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo)

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