Search in sources :

Example 26 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class ErrorTypeLocator method lookupComponentErrorType.

/**
 * Finds the {@code ErrorType} related to a component defined by the {@link ComponentIdentifier} based on the exception thrown
 * by the component and the mappings configured in the {@code ErrorTypeLocator}.
 *
 * If no mapping is available then the {@link #lookupErrorType(Throwable)} rules applies.
 *
 * @param componentIdentifier the identifier of the component that throw the exception.
 * @param exception the exception thrown by the component.
 * @return the error type related to the exception based on the component mappings. If there's no mapping then the error type
 *         related to UNKNOWN will be returned.
 */
public ErrorType lookupComponentErrorType(ComponentIdentifier componentIdentifier, Class<? extends Throwable> exception) {
    ExceptionMapper exceptionMapper = componentExceptionMappers.get(componentIdentifier);
    Optional<ErrorType> errorType = empty();
    if (exceptionMapper != null) {
        errorType = exceptionMapper.resolveErrorType(exception);
    }
    return errorType.orElseGet(() -> lookupErrorType(exception));
}
Also used : ExceptionMapper(org.mule.runtime.core.api.exception.ExceptionMapper) ErrorType(org.mule.runtime.api.message.ErrorType)

Example 27 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class ModuleExceptionHandler method isAllowedError.

private boolean isAllowedError(ErrorType errorType) {
    return allowedErrorTypes.stream().anyMatch(errorModel -> {
        boolean isAllowed = false;
        ErrorType currentError = errorType;
        while (currentError != null && !isAllowed) {
            isAllowed = errorModel.getType().equals(currentError.getIdentifier()) && errorModel.getNamespace().equals(currentError.getNamespace());
            currentError = currentError.getParentErrorType();
        }
        return isAllowed;
    });
}
Also used : ErrorType(org.mule.runtime.api.message.ErrorType)

Example 28 with ErrorType

use of org.mule.runtime.api.message.ErrorType 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)

Example 29 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class ModuleExceptionHandlerTestCase method handleTypedException.

@Test
public void handleTypedException() {
    when(operationModel.getErrorModels()).thenReturn(singleton(newError(CONNECTIVITY_ERROR_IDENTIFIER, ERROR_NAMESPACE).build()));
    ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository);
    typeRepository.addErrorType(builder().name(CONNECTIVITY_ERROR_IDENTIFIER).namespace(ERROR_NAMESPACE).build(), typeRepository.getAnyErrorType());
    ModuleException moduleException = new ModuleException(CONNECTIVITY, new RuntimeException());
    Throwable exception = handler.processException(moduleException);
    assertThat(exception, is(instanceOf(TypedException.class)));
    ErrorType errorType = ((TypedException) exception).getErrorType();
    assertThat(errorType.getIdentifier(), is(CONNECTIVITY_ERROR_IDENTIFIER));
    assertThat(errorType.getNamespace(), is(ERROR_NAMESPACE));
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ErrorType(org.mule.runtime.api.message.ErrorType) ModuleException(org.mule.runtime.extension.api.exception.ModuleException) TypedException(org.mule.runtime.api.exception.TypedException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 30 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class ModuleExceptionHandlerTestCase method handleThrowingChildErrorsFromTheOneDeclared.

@Test
public void handleThrowingChildErrorsFromTheOneDeclared() {
    Set<ErrorModel> errors = new HashSet<>();
    ErrorModel parent = newError(PARENT.getType(), ERROR_NAMESPACE).build();
    ErrorModel child = newError(CHILD.getType(), ERROR_NAMESPACE).withParent(parent).build();
    errors.add(parent);
    ErrorType parentErrorType = typeRepository.addErrorType(getIdentifier(parent), typeRepository.getAnyErrorType());
    typeRepository.addErrorType(getIdentifier(child), parentErrorType);
    when(operationModel.getErrorModels()).thenReturn(errors);
    ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository);
    ModuleException moduleException = new ModuleException(CHILD, new RuntimeException());
    Throwable throwable = handler.processException(moduleException);
    assertThat(throwable, is(instanceOf(TypedException.class)));
    ErrorType errorType = ((TypedException) throwable).getErrorType();
    assertThat(errorType.getIdentifier(), is(CHILD.getType()));
    assertThat(errorType.getNamespace(), is(ERROR_NAMESPACE));
}
Also used : MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ErrorType(org.mule.runtime.api.message.ErrorType) ErrorModel(org.mule.runtime.api.meta.model.error.ErrorModel) ModuleException(org.mule.runtime.extension.api.exception.ModuleException) TypedException(org.mule.runtime.api.exception.TypedException) HashSet(java.util.HashSet) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

ErrorType (org.mule.runtime.api.message.ErrorType)35 Test (org.junit.Test)19 SmallTest (org.mule.tck.size.SmallTest)9 SingleErrorTypeMatcher (org.mule.runtime.core.api.exception.SingleErrorTypeMatcher)8 Error (org.mule.runtime.api.message.Error)7 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)6 ErrorTypeMatcher (org.mule.runtime.core.api.exception.ErrorTypeMatcher)6 ErrorTypeLocator (org.mule.runtime.core.privileged.exception.ErrorTypeLocator)6 ErrorTypeRepository (org.mule.runtime.api.exception.ErrorTypeRepository)5 Optional (java.util.Optional)4 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)4 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)4 PrivilegedMuleContext (org.mule.runtime.core.privileged.PrivilegedMuleContext)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Component (org.mule.runtime.api.component.Component)3 TypedException (org.mule.runtime.api.exception.TypedException)3 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 HashSet (java.util.HashSet)2