Search in sources :

Example 1 with ErrorMapping

use of org.mule.runtime.core.internal.exception.ErrorMapping 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();
}
Also used : ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) Collections.emptyList(java.util.Collections.emptyList) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) Component(org.mule.runtime.api.component.Component) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ANNOTATION_ERROR_MAPPINGS(org.mule.runtime.core.internal.exception.ErrorMapping.ANNOTATION_ERROR_MAPPINGS) Error(org.mule.runtime.api.message.Error) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Error(org.mule.runtime.api.message.Error)

Example 2 with ErrorMapping

use of org.mule.runtime.core.internal.exception.ErrorMapping 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 3 with ErrorMapping

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

the class InternalExceptionUtils method createErrorEvent.

/**
 * Create new {@link CoreEvent} with {@link org.mule.runtime.api.message.Error} instance set.
 *
 * @param currentEvent event when error occured.
 * @param obj message processor/source.
 * @param me messaging exception.
 * @param locator the mule context.
 * @return new {@link CoreEvent} with relevant {@link org.mule.runtime.api.message.Error} set.
 */
public static CoreEvent createErrorEvent(CoreEvent currentEvent, Component obj, MessagingException me, ErrorTypeLocator locator) {
    Throwable cause = me.getCause() != null ? me.getCause() : me;
    List<ErrorMapping> errorMappings = getErrorMappings(obj);
    if (!errorMappings.isEmpty() || isMessagingExceptionCause(me, cause)) {
        Error newError = getErrorFromFailingProcessor(currentEvent, obj, cause, locator);
        CoreEvent newEvent = CoreEvent.builder(me.getEvent()).error(newError).build();
        me.setProcessedEvent(newEvent);
        return newEvent;
    } else {
        return currentEvent;
    }
}
Also used : ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Error(org.mule.runtime.api.message.Error)

Example 4 with ErrorMapping

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

the class BeanDefinitionFactory method resolveComponent.

private BeanDefinition resolveComponent(ComponentModel parentComponentModel, SpringComponentModel componentModel, BeanDefinitionRegistry registry, BiConsumer<ComponentModel, BeanDefinitionRegistry> componentDefinitionModelProcessor, SpringConfigurationComponentLocator componentLocator) {
    if (isComponentIgnored(componentModel.getIdentifier())) {
        return null;
    }
    if (!componentModel.isEnabled()) {
        // Just register the location, for support of lazyInit scenarios
        componentLocator.addComponentLocation(componentModel.getComponentLocation());
        return null;
    }
    resolveComponentBeanDefinition(parentComponentModel, componentModel);
    componentDefinitionModelProcessor.accept(componentModel, registry);
    // TODO MULE-9638: Once we migrate all core definitions we need to define a mechanism for customizing
    // how core constructs are processed.
    processMuleConfiguration(componentModel, registry);
    processMuleSecurityManager(componentModel, registry);
    processRaiseError(componentModel);
    componentBuildingDefinitionRegistry.getBuildingDefinition(componentModel.getIdentifier()).ifPresent(componentBuildingDefinition -> {
        if ((componentModel.getType() != null) && Component.class.isAssignableFrom(componentModel.getType())) {
            addAnnotation(ANNOTATION_NAME, componentModel.getIdentifier(), componentModel);
            // We need to use a mutable map since spring will resolve the properties placeholder present in the value if needed
            // and it will be done by mutating the same map.
            addAnnotation(ANNOTATION_PARAMETERS, new HashMap<>(componentModel.getParameters()), componentModel);
            // add any error mappings if present
            List<ComponentModel> errorMappingComponents = componentModel.getInnerComponents().stream().filter(innerComponent -> ERROR_MAPPING_IDENTIFIER.equals(innerComponent.getIdentifier())).collect(toList());
            if (!errorMappingComponents.isEmpty()) {
                addAnnotation(ANNOTATION_ERROR_MAPPINGS, errorMappingComponents.stream().map(innerComponent -> {
                    Map<String, String> parameters = innerComponent.getParameters();
                    ComponentIdentifier source = buildFromStringRepresentation(parameters.get(SOURCE_TYPE));
                    ErrorType errorType = errorTypeRepository.lookupErrorType(source).orElseThrow(() -> new MuleRuntimeException(createStaticMessage("Could not find error '%s'.", source)));
                    ErrorTypeMatcher errorTypeMatcher = new SingleErrorTypeMatcher(errorType);
                    ErrorType targetValue = resolveErrorType(parameters.get(TARGET_TYPE));
                    return new ErrorMapping(errorTypeMatcher, targetValue);
                }).collect(toList()), componentModel);
            }
            componentLocator.addComponentLocation(componentModel.getComponentLocation());
        }
    });
    addAnnotation(LOCATION_KEY, componentModel.getComponentLocation(), componentModel);
    BeanDefinition beanDefinition = componentModel.getBeanDefinition();
    return beanDefinition;
}
Also used : KeyAttributeDefinitionPair(org.mule.runtime.dsl.api.component.KeyAttributeDefinitionPair) CORE_PREFIX(org.mule.runtime.internal.dsl.DslConstants.CORE_PREFIX) BeanReference(org.springframework.beans.factory.config.BeanReference) Optional.of(java.util.Optional.of) BiFunction(java.util.function.BiFunction) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) DESCRIPTION_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.DESCRIPTION_IDENTIFIER) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) SECURITY_MANAGER_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.SECURITY_MANAGER_IDENTIFIER) MULE_PROPERTY_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTY_IDENTIFIER) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) Map(java.util.Map) MULE_EE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_EE_DOMAIN_IDENTIFIER) DEFAULT_OBJECT_SERIALIZER_NAME(org.mule.runtime.api.serialization.ObjectSerializer.DEFAULT_OBJECT_SERIALIZER_NAME) RAISE_ERROR_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.RAISE_ERROR_IDENTIFIER) MULE_DOMAIN_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_DOMAIN_IDENTIFIER) ANNOTATIONS_ELEMENT_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.ANNOTATIONS_ELEMENT_IDENTIFIER) ERROR_MAPPING_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.ERROR_MAPPING_IDENTIFIER) ImmutableSet(com.google.common.collect.ImmutableSet) SpringConfigurationComponentLocator(org.mule.runtime.config.internal.SpringConfigurationComponentLocator) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ServiceLoader(java.util.ServiceLoader) CommonBeanDefinitionCreator.areMatchingTypes(org.mule.runtime.config.internal.dsl.spring.CommonBeanDefinitionCreator.areMatchingTypes) String.format(java.lang.String.format) ComponentBuildingDefinition(org.mule.runtime.dsl.api.component.ComponentBuildingDefinition) List(java.util.List) ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) ANNOTATION_NAME(org.mule.runtime.core.internal.component.ComponentAnnotations.ANNOTATION_NAME) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ANNOTATION_PARAMETERS(org.mule.runtime.core.internal.component.ComponentAnnotations.ANNOTATION_PARAMETERS) DOC_DESCRIPTION_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.DOC_DESCRIPTION_IDENTIFIER) RetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.RetryPolicyTemplate) ComponentBuildingDefinitionRegistry(org.mule.runtime.config.api.dsl.model.ComponentBuildingDefinitionRegistry) OBJECT_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.OBJECT_IDENTIFIER) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) ComponentIdentifier.buildFromStringRepresentation(org.mule.runtime.api.component.ComponentIdentifier.buildFromStringRepresentation) OBJECT_EXPRESSION_LANGUAGE(org.mule.runtime.core.api.config.MuleProperties.OBJECT_EXPRESSION_LANGUAGE) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) MAP(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.MAP) MULE_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.MULE_IDENTIFIER) SINGLE(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.SINGLE) COLLECTION(org.mule.runtime.config.internal.dsl.spring.WrapperElementType.COLLECTION) Component(org.mule.runtime.api.component.Component) BiConsumer(java.util.function.BiConsumer) ComponentModelHelper.addAnnotation(org.mule.runtime.config.internal.dsl.spring.ComponentModelHelper.addAnnotation) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) OBJECT_DEFAULT_RETRY_POLICY_TEMPLATE(org.mule.runtime.core.api.config.MuleProperties.OBJECT_DEFAULT_RETRY_POLICY_TEMPLATE) LOCATION_KEY(org.mule.runtime.api.component.AbstractComponent.LOCATION_KEY) MULE_PROPERTIES_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.MULE_PROPERTIES_IDENTIFIER) GLOBAL_PROPERTY_IDENTIFIER(org.mule.runtime.config.internal.model.ApplicationModel.GLOBAL_PROPERTY_IDENTIFIER) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) AttributeDefinition(org.mule.runtime.dsl.api.component.AttributeDefinition) AbstractAttributeDefinitionVisitor(org.mule.runtime.config.api.dsl.processor.AbstractAttributeDefinitionVisitor) ErrorTypeMatcher(org.mule.runtime.core.api.exception.ErrorTypeMatcher) Consumer(java.util.function.Consumer) Either(org.mule.runtime.core.api.functional.Either) Collectors.toList(java.util.stream.Collectors.toList) ConfigurationPropertiesProviderFactory(org.mule.runtime.config.api.dsl.model.properties.ConfigurationPropertiesProviderFactory) Element(org.w3c.dom.Element) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) CONFIGURATION_IDENTIFIER(org.mule.runtime.config.api.dsl.CoreDslConstants.CONFIGURATION_IDENTIFIER) ANNOTATION_ERROR_MAPPINGS(org.mule.runtime.core.internal.exception.ErrorMapping.ANNOTATION_ERROR_MAPPINGS) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) ErrorTypeMatcher(org.mule.runtime.core.api.exception.ErrorTypeMatcher) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorType(org.mule.runtime.api.message.ErrorType) SpringComponentModel(org.mule.runtime.config.internal.dsl.model.SpringComponentModel) ComponentModel(org.mule.runtime.config.internal.model.ComponentModel) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component)

Aggregations

ErrorMapping (org.mule.runtime.core.internal.exception.ErrorMapping)4 List (java.util.List)3 Component (org.mule.runtime.api.component.Component)3 Error (org.mule.runtime.api.message.Error)3 ErrorType (org.mule.runtime.api.message.ErrorType)3 Optional (java.util.Optional)2 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 ExceptionUtils.getComponentIdentifier (org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier)2 ExceptionUtils.isUnknownMuleError (org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 ErrorBuilder (org.mule.runtime.core.internal.message.ErrorBuilder)2 ErrorTypeLocator (org.mule.runtime.core.privileged.exception.ErrorTypeLocator)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 String.format (java.lang.String.format)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1