private void initErrorModelMap(Map<String, ErrorModel> errorModelMap) { errorModelMap.put(toIdentifier(ANY), newError(ANY.getType(), MULE).build()); } }
private void declareErrorModels(OperationDeclarer operationDeclarer, XmlDslModel xmlDslModel, String operationName, ComponentModel operationModel) { Optional<ComponentModel> optionalParametersComponentModel = operationModel.getInnerComponents() .stream() .filter(child -> child.getIdentifier().equals(OPERATION_ERRORS_IDENTIFIER)).findAny(); optionalParametersComponentModel.ifPresent(componentModel -> componentModel.getInnerComponents() .stream() .filter(child -> child.getIdentifier().equals(OPERATION_ERROR_IDENTIFIER)) .forEach(param -> { final String namespace = xmlDslModel.getPrefix().toUpperCase(); final String typeName = param.getParameters().get(ERROR_TYPE_ATTRIBUTE); if (StringUtils.isBlank(typeName)) { throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> with an empty 'type' attribute", operationName)); } if (typeName.contains(NAMESPACE_SEPARATOR)) { throw new IllegalModelDefinitionException(format("The operation [%s] cannot have an <error> [%s] that contains a reserved character [%s]", operationName, typeName, NAMESPACE_SEPARATOR)); } operationDeclarer.withErrorModel(ErrorModelBuilder.newError(typeName, namespace) .withParent(ErrorModelBuilder.newError(ANY).build()) .build()); })); }
/** * @param errorTypeDefinition * @param errorModelMap * @return */ private ErrorModel toErrorModel(ErrorTypeDefinition<?> errorTypeDefinition, Map<String, ErrorModel> errorModelMap) { if (errorModelMap.containsKey(toIdentifier(errorTypeDefinition))) { return errorModelMap.get(toIdentifier(errorTypeDefinition)); } else { ErrorModelBuilder builder = newError(errorTypeDefinition.getType(), getErrorNamespace(errorTypeDefinition)); builder.withParent(toErrorModel(errorTypeDefinition.getParent().orElse(ANY), errorModelMap)); ErrorModel errorModel = builder.build(); errorModelMap.put(toIdentifier(errorTypeDefinition), errorModel); return errorModel; } }
@Test public void handleThrowingOfNotRegisteredErrorType() { when(operationModel.getErrorModels()).thenReturn(singleton(newError(CONNECTIVITY_ERROR_IDENTIFIER, ERROR_NAMESPACE).build())); ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository); ModuleException moduleException = new ModuleException(CONNECTIVITY, new RuntimeException()); assertThatThrownBy(() -> handler.processException(moduleException)) .isInstanceOf(MuleRuntimeException.class) .hasMessage("The component 'testOperation' from the connector 'Test Extension' attempted to throw 'TEST-EXTENSION:CONNECTIVITY'," + " but it was not registered in the Error Repository"); }
@Test public void operationTriesToAddInternalErrorType() { ErrorTypeRepository repository = mock(ErrorTypeRepository.class); when(repository.getErrorType(any())).then((e) -> typeRepository.getErrorType(((ComponentIdentifier) e.getArguments()[0]))); ErrorModel internalRepeatedError = ErrorModelBuilder.newError(SOURCE_RESPONSE_GENERATE).build(); when(operationWithError.getErrorModels()).thenReturn(singleton(internalRepeatedError)); when(extensionModel.getOperationModels()).thenReturn(singletonList(operationWithError)); when(extensionModel.getErrorModels()).thenReturn(singleton(internalRepeatedError)); ErrorTypeLocator mockTypeLocator = mock(ErrorTypeLocator.class); errorsRegistrant = new ExtensionErrorsRegistrant(typeRepository, mockTypeLocator); errorsRegistrant.registerErrors(extensionModel); verify(repository, times(0)).addErrorType(any(), any()); }
@Test public void handleThrowingOfNotDeclaredErrorType() { typeRepository.addErrorType(buildFromStringRepresentation(ERROR_NAMESPACE + ":" + CONNECTIVITY_ERROR_IDENTIFIER), typeRepository.getAnyErrorType()); when(operationModel.getErrorModels()) .thenReturn(singleton(newError(TRANSFORMATION_ERROR_IDENTIFIER, ERROR_NAMESPACE).build())); ModuleExceptionHandler handler = new ModuleExceptionHandler(operationModel, extensionModel, typeRepository); ModuleException moduleException = new ModuleException(CONNECTIVITY, new RuntimeException()); assertThatThrownBy(() -> handler.processException(moduleException)) .isInstanceOf(MuleRuntimeException.class) .hasMessage("The component 'testOperation' from the connector 'Test Extension' attempted to throw 'TEST-EXTENSION:CONNECTIVITY', " + "but only [TEST-EXTENSION:TRANSFORMATION] errors are allowed."); }
@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)); }
@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)); }
hasItem(newError(TRANSFORMATION).withParent(errorMuleAny).build()));
private ErrorModel buildSimpleError(ComponentIdentifier identifier, Map<String, ErrorModel> builtErrors) { ErrorModel errorModel = newError(identifier).build(); builtErrors.put(identifier.toString(), errorModel); return errorModel; } }
private static ErrorModel createErrorModel(String errorIdentifier) { ComponentIdentifier componentIdentifier = buildFromStringRepresentation(errorIdentifier); return newError(componentIdentifier).build(); } }
private void initErrorModelMap(Map<String, ErrorModel> errorModelMap) { errorModelMap.put(toIdentifier(ANY), newError(ANY.getType(), MULE).build()); } }
private ErrorModel buildError(String errorIdentifier, Map<String, Pair<String, ErrorModelBuilder>> buildingErrors, Map<String, ErrorModel> builtErrorModels) { if (builtErrorModels.containsKey(errorIdentifier)) { return builtErrorModels.get(errorIdentifier); } else { Pair<String, ErrorModelBuilder> builderPair = buildingErrors.get(errorIdentifier); if (builderPair != null) { String parentError = builderPair.getFirst(); ErrorModel errorModel; ErrorModelBuilder errorBuilder = builderPair.getSecond(); if (!isEmpty(parentError)) { errorModel = errorBuilder.withParent(buildError(parentError, buildingErrors, builtErrorModels)).build(); } else { errorModel = errorBuilder.build(); } builtErrorModels.put(errorIdentifier, errorModel); return errorModel; } else { ComponentIdentifier identifier = buildFromStringRepresentation(errorIdentifier); return buildSimpleError(identifier, builtErrorModels); } } }
/** * @param errorTypeDefinition * @param errorModelMap * @return */ private ErrorModel toErrorModel(ErrorTypeDefinition<?> errorTypeDefinition, Map<String, ErrorModel> errorModelMap) { if (errorModelMap.containsKey(toIdentifier(errorTypeDefinition))) { return errorModelMap.get(toIdentifier(errorTypeDefinition)); } else { ErrorModelBuilder builder = newError(errorTypeDefinition.getType(), getErrorNamespace(errorTypeDefinition)); builder.withParent(toErrorModel(errorTypeDefinition.getParent().orElse(ANY), errorModelMap)); ErrorModel errorModel = builder.build(); errorModelMap.put(toIdentifier(errorTypeDefinition), errorModel); return errorModel; } }
private void declareErrors(ExtensionDeclarer extensionDeclarer) { final ErrorModel criticalError = newError(CRITICAL).handleable(false).build(); final ErrorModel overloadError = newError(OVERLOAD).withParent(criticalError).build(); final ErrorModel securityError = newError(SECURITY).withParent(anyError).build(); final ErrorModel sourceError = newError(SOURCE).withParent(anyError).build(); final ErrorModel sourceResponseError = newError(SOURCE_RESPONSE).withParent(anyError).build(); final ErrorModel serverSecurityError = newError(SERVER_SECURITY).withParent(securityError).build(); extensionDeclarer.withErrorModel(newError(EXPRESSION).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(TRANSFORMATION).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(CONNECTIVITY).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(RETRY_EXHAUSTED).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(REDELIVERY_EXHAUSTED).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(STREAM_MAXIMUM_SIZE_EXCEEDED).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(TIMEOUT).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(UNKNOWN).handleable(false).withParent(anyError).build()); extensionDeclarer.withErrorModel(newError(CLIENT_SECURITY).withParent(securityError).build()); extensionDeclarer.withErrorModel(newError(NOT_PERMITTED).withParent(securityError).build()); extensionDeclarer.withErrorModel(newError(SOURCE_ERROR_RESPONSE_GENERATE).handleable(false).withParent(sourceError).build()); extensionDeclarer.withErrorModel(newError(SOURCE_ERROR_RESPONSE_SEND).handleable(false).withParent(sourceError).build()); extensionDeclarer.withErrorModel(newError(SOURCE_RESPONSE_GENERATE).withParent(sourceResponseError).build()); extensionDeclarer.withErrorModel(newError(SOURCE_RESPONSE_SEND).handleable(false).withParent(sourceResponseError).build()); extensionDeclarer.withErrorModel(newError(FLOW_BACK_PRESSURE).handleable(false).withParent(overloadError).build()); extensionDeclarer.withErrorModel(newError(FATAL).handleable(false).withParent(criticalError).build());
hasItem(newError(TRANSFORMATION).withParent(errorMuleAny).build()));