use of org.mule.runtime.core.api.event.CoreEvent in project mule by mulesoft.
the class SetPayloadMessageProcessor method process.
@Override
public CoreEvent process(CoreEvent event) throws MuleException {
final Message.Builder builder = Message.builder(event.getMessage());
final CoreEvent.Builder eventBuilder = CoreEvent.builder(event);
if (dataType == null) {
final TypedValue typedValue = resolveTypedValue(event);
builder.value(typedValue.getValue()).mediaType(typedValue.getDataType().getMediaType());
} else {
Object value = resolveValue(event);
final DataTypeParamsBuilder dataTypeBuilder = DataType.builder(dataType).type(value == null ? Object.class : value.getClass());
builder.value(value).mediaType(dataTypeBuilder.build().getMediaType());
}
return eventBuilder.message(builder.build()).build();
}
use of org.mule.runtime.core.api.event.CoreEvent in project mule by mulesoft.
the class CompositeSourcePolicy method processNextOperation.
/**
* Executes the flow.
*
* If there's a {@link SourcePolicyParametersTransformer} provided then it will use it to convert the source response or source
* failure response from the parameters back to a {@link Message} that can be routed through the policy chain which later will
* be convert back to response or failure response parameters thus allowing the policy chain to modify the response.. That
* message will be the result of the next-operation of the policy.
*
* If no {@link SourcePolicyParametersTransformer} is provided, then the same response from the flow is going to be routed as
* response of the next-operation of the policy chain. In this case, the same response from the flow is going to be used to
* generate the response or failure response for the source so the policy chain is not going to be able to modify the response
* sent by the source.
*
* When the flow execution fails, it will create a {@link FlowExecutionException} instead of a regular
* {@link MessagingException} to signal that the failure was through the the flow exception and not the policy logic.
*/
@Override
protected Publisher<CoreEvent> processNextOperation(CoreEvent event) {
return just(event).flatMap(request -> from(processWithChildContext(request, flowExecutionProcessor, empty()))).map(flowExecutionResponse -> {
originalResponseParameters = getParametersProcessor().getSuccessfulExecutionResponseParametersFunction().apply(flowExecutionResponse);
Message message = getParametersTransformer().map(parametersTransformer -> parametersTransformer.fromSuccessResponseParametersToMessage(originalResponseParameters)).orElseGet(flowExecutionResponse::getMessage);
return CoreEvent.builder(event).message(message).build();
}).onErrorMap(MessagingException.class, messagingException -> {
originalFailureResponseParameters = getParametersProcessor().getFailedExecutionResponseParametersFunction().apply(messagingException.getEvent());
Message message = getParametersTransformer().map(parametersTransformer -> parametersTransformer.fromFailureResponseParametersToMessage(originalFailureResponseParameters)).orElse(messagingException.getEvent().getMessage());
MessagingException flowExecutionException = new FlowExecutionException(CoreEvent.builder(messagingException.getEvent()).message(message).build(), messagingException.getCause(), messagingException.getFailingComponent());
if (messagingException.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
flowExecutionException.addInfo(INFO_ALREADY_LOGGED_KEY, messagingException.getInfo().get(INFO_ALREADY_LOGGED_KEY));
}
return flowExecutionException;
}).doOnError(e -> LOGGER.error(e.getMessage(), e));
}
use of org.mule.runtime.core.api.event.CoreEvent in project mule by mulesoft.
the class InternalExceptionUtils method getErrorFromFailingProcessor.
/**
* Determine the {@link ErrorType} of a given exception thrown by a given message processor.
*
* @param processor the component that threw the exception (processor or source).
* @param cause the exception thrown.
* @param locator the {@link ErrorTypeLocator}.
* @return the resolved {@link ErrorType}
*/
public static Error getErrorFromFailingProcessor(CoreEvent currentEvent, Component processor, Throwable cause, ErrorTypeLocator locator) {
ErrorType currentError = currentEvent != null ? currentEvent.getError().map(Error::getErrorType).orElse(null) : null;
ErrorType foundErrorType = locator.lookupErrorType(cause);
ErrorType resultError = isUnknownMuleError(foundErrorType) ? currentError : foundErrorType;
ErrorType errorType = getComponentIdentifier(processor).map(ci -> locator.lookupComponentErrorType(ci, cause)).orElse(locator.lookupErrorType(cause));
return ErrorBuilder.builder(cause).errorType(getErrorMappings(processor).stream().filter(m -> m.match(resultError == null || isUnknownMuleError(resultError) ? errorType : currentError)).findFirst().map(ErrorMapping::getTarget).orElse(errorType)).build();
}
use of org.mule.runtime.core.api.event.CoreEvent 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);
}
use of org.mule.runtime.core.api.event.CoreEvent 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);
}
Aggregations