Search in sources :

Example 1 with ComponentExecutionException

use of org.mule.runtime.api.component.execution.ComponentExecutionException 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 2 with ComponentExecutionException

use of org.mule.runtime.api.component.execution.ComponentExecutionException in project mule by mulesoft.

the class LookupFunction method call.

@Override
public Object call(Object[] parameters, BindingContext context) {
    String flowName = (String) parameters[0];
    Object payload = parameters[1];
    Location componentLocation = Location.builder().globalName(flowName).build();
    Component component = componentLocator.find(componentLocation).orElseThrow(() -> new IllegalArgumentException(format("There is no component named '%s'.", flowName)));
    if (component instanceof Flow) {
        try {
            Message incomingMessage = lookupValue(context, MESSAGE, Message.builder().nullValue().build());
            Map<String, ?> incomingVariables = lookupValue(context, VARS, EMPTY_MAP);
            Error incomingError = lookupValue(context, ERROR, null);
            Message message = Message.builder(incomingMessage).value(payload).mediaType(APPLICATION_JAVA).build();
            CoreEvent event = CoreEvent.builder(PrivilegedEvent.getCurrentEvent().getContext()).variables(incomingVariables).error(incomingError).message(message).build();
            return ((ExecutableComponent) component).execute(event).get().getMessage().getPayload();
        } catch (ExecutionException e) {
            ComponentExecutionException componentExecutionException = (ComponentExecutionException) e.getCause();
            Error error = componentExecutionException.getEvent().getError().get();
            throw new MuleRuntimeException(createStaticMessage(format("Flow '%s' has failed with error '%s' (%s)", flowName, error.getErrorType(), error.getDescription())), error.getCause());
        } catch (InterruptedException e) {
            throw new MuleRuntimeException(e);
        }
    } else {
        throw new IllegalArgumentException(format("Component '%s' is not a flow.", flowName));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Error(org.mule.runtime.api.message.Error) Flow(org.mule.runtime.core.api.construct.Flow) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) ExecutionException(java.util.concurrent.ExecutionException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Location(org.mule.runtime.api.component.location.Location)

Example 3 with ComponentExecutionException

use of org.mule.runtime.api.component.execution.ComponentExecutionException in project mule by mulesoft.

the class AbstractExecutableComponent method execute.

@Override
public final CompletableFuture<Event> execute(Event event) {
    CoreEvent internalEvent;
    BaseEventContext child = createChildEventContext(event.getContext());
    if (event instanceof CoreEvent) {
        internalEvent = builder(child, (CoreEvent) event).build();
    } else {
        internalEvent = CoreEvent.builder(createEventContext(null)).message(event.getMessage()).error(event.getError().orElse(null)).variables(event.getVariables()).build();
    }
    return from(MessageProcessors.process(internalEvent, getExecutableFunction())).onErrorMap(throwable -> {
        MessagingException messagingException = (MessagingException) throwable;
        CoreEvent messagingExceptionEvent = messagingException.getEvent();
        return new ComponentExecutionException(messagingExceptionEvent.getError().get().getCause(), messagingExceptionEvent);
    }).map(r -> builder(event.getContext(), r).build()).cast(Event.class).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) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Event(org.mule.runtime.api.event.Event) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) InputEvent(org.mule.runtime.api.component.execution.InputEvent)

Aggregations

ComponentExecutionException (org.mule.runtime.api.component.execution.ComponentExecutionException)3 ExecutableComponent (org.mule.runtime.api.component.execution.ExecutableComponent)3 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)3 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)3 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 String.format (java.lang.String.format)2 Optional (java.util.Optional)2 Optional.of (java.util.Optional.of)2 Optional.ofNullable (java.util.Optional.ofNullable)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Function (java.util.function.Function)2 Inject (javax.inject.Inject)2 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)2 ExecutionResult (org.mule.runtime.api.component.execution.ExecutionResult)2 InputEvent (org.mule.runtime.api.component.execution.InputEvent)2 Event (org.mule.runtime.api.event.Event)2 EventContext (org.mule.runtime.api.event.EventContext)2 MuleContext (org.mule.runtime.core.api.MuleContext)2 CoreEvent.builder (org.mule.runtime.core.api.event.CoreEvent.builder)2 EventContextFactory.create (org.mule.runtime.core.api.event.EventContextFactory.create)2