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));
}
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;
});
}
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;
}
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));
}
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));
}
Aggregations