private ErrorType createErrorType(ErrorModel errorModel, ComponentIdentifier identifier, ExtensionModel extensionModel) { final ErrorType errorType; if (identifier.getNamespace().equals(MULE)) { throw new MuleRuntimeException(createStaticMessage(format("The extension [%s] tried to register the [%s] error with [%s] namespace, which is not allowed.", extensionModel.getName(), identifier, MULE))); } if (errorModel.getParent().isPresent()) { errorType = errorTypeRepository.addErrorType(identifier, getErrorType(errorModel.getParent().get(), extensionModel)); } else { errorType = errorTypeRepository.addErrorType(identifier, null); } return errorType; }
private ErrorType resolveErrorType(String representation) { int separator = representation.indexOf(":"); String namespace; String identifier; if (separator > 0) { namespace = representation.substring(0, separator).toUpperCase(); identifier = representation.substring(separator + 1).toUpperCase(); } else { namespace = CORE_ERROR_NS; identifier = representation.toUpperCase(); } ComponentIdentifier errorIdentifier = ComponentIdentifier.builder().namespace(namespace).name(identifier).build(); if (CORE_ERROR_NS.equals(namespace)) { return errorTypeRepository.lookupErrorType(errorIdentifier) .orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("There's no MULE error named '%s'.", identifier)))); } else if (errorTypeRepository.getErrorNamespaces().contains(namespace) && !syntheticErrorNamespaces.contains(namespace)) { throw new MuleRuntimeException(createStaticMessage(format("Cannot use error type '%s:%s': namespace already exists.", namespace, identifier))); } else if (syntheticErrorNamespaces.contains(namespace)) { Optional<ErrorType> optionalErrorType = errorTypeRepository.lookupErrorType(errorIdentifier); if (optionalErrorType.isPresent()) { return optionalErrorType.get(); } } else { syntheticErrorNamespaces.add(namespace); } return errorTypeRepository.addErrorType(errorIdentifier, errorTypeRepository.getAnyErrorType()); }
@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 doesNotAddInternalTypeAsRegularOne() { expectedException.expectMessage(is("An error type with identifier 'NS:NAME' already exists")); errorTypeRepository.addErrorType(INTERNAL_ERROR, errorTypeRepository.getAnyErrorType()); }
@Before public void setUp() { errorTypeRepository.addErrorType(CONNECTIVITY, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(ROUTING, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(EXPOSED_ERROR, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addInternalErrorType(INTERNAL_ERROR, errorTypeRepository.getCriticalErrorType()); errorTypeRepository.addInternalErrorType(OTHER_INTERNAL_ERROR, errorTypeRepository.getCriticalErrorType()); }
@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 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)); }
@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)); }
/** * {@inheritDoc} */ @Override public ErrorType addErrorType(ComponentIdentifier errorTypeIdentifier, ErrorType parentErrorType) { return childErrorTypeRepository.addErrorType(errorTypeIdentifier, parentErrorType); }
@Test public void matchChild() { ComponentIdentifier customTransformerIdentifier = ComponentIdentifier.builder().name("custom").namespace(CORE_NAMESPACE_NAME).build(); ErrorTypeRepository errorTypeRepository = muleContext.getErrorTypeRepository(); ErrorType customTransformerErrorType = errorTypeRepository.addErrorType(customTransformerIdentifier, transformationErrorType); ErrorTypeMatcher transformationMatcher = new SingleErrorTypeMatcher(transformationErrorType); assertThat(transformationMatcher.match(customTransformerErrorType), is(true)); }
private ErrorType createErrorType(ErrorModel errorModel, ComponentIdentifier identifier, ExtensionModel extensionModel) { final ErrorType errorType; if (identifier.getNamespace().equals(MULE)) { throw new MuleRuntimeException(createStaticMessage(format("The extension [%s] tried to register the [%s] error with [%s] namespace, which is not allowed.", extensionModel.getName(), identifier, MULE))); } if (errorModel.getParent().isPresent()) { errorType = errorTypeRepository.addErrorType(identifier, getErrorType(errorModel.getParent().get(), extensionModel)); } else { errorType = errorTypeRepository.addErrorType(identifier, null); } return errorType; }
errorTypeRepository.addErrorType(TRANSFORMATION, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(EXPRESSION, errorTypeRepository.getAnyErrorType()); final ErrorType validationErrorType = errorTypeRepository.addErrorType(VALIDATION, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(DUPLICATE_MESSAGE, validationErrorType); errorTypeRepository.addErrorType(REDELIVERY_EXHAUSTED, errorTypeRepository.getAnyErrorType()); final ErrorType connectivityErrorType = errorTypeRepository.addErrorType(CONNECTIVITY, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(RETRY_EXHAUSTED, connectivityErrorType); errorTypeRepository.addErrorType(ROUTING, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(SECURITY, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(CLIENT_SECURITY, errorTypeRepository.getErrorType(SECURITY).get()); errorTypeRepository.addErrorType(SERVER_SECURITY, errorTypeRepository.getErrorType(SECURITY).get()); errorTypeRepository.addErrorType(NOT_PERMITTED, errorTypeRepository.getErrorType(SERVER_SECURITY).get()); errorTypeRepository.addErrorType(STREAM_MAXIMUM_SIZE_EXCEEDED, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(TIMEOUT, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(COMPOSITE_ROUTING, errorTypeRepository.getErrorType(ROUTING).get()); errorTypeRepository.addErrorType(SOURCE_RESPONSE_GENERATE, errorTypeRepository.getSourceResponseErrorType()); errorTypeRepository.addErrorType(SOURCE_RESPONSE_SEND, errorTypeRepository.getSourceResponseErrorType()); errorTypeRepository.addInternalErrorType(SOURCE_ERROR_RESPONSE_GENERATE, sourceErrorType); errorTypeRepository.addInternalErrorType(SOURCE_ERROR_RESPONSE_SEND, sourceErrorType);
private ErrorType resolveErrorType(String representation) { int separator = representation.indexOf(":"); String namespace; String identifier; if (separator > 0) { namespace = representation.substring(0, separator).toUpperCase(); identifier = representation.substring(separator + 1).toUpperCase(); } else { namespace = CORE_ERROR_NS; identifier = representation.toUpperCase(); } ComponentIdentifier errorIdentifier = ComponentIdentifier.builder().namespace(namespace).name(identifier).build(); if (CORE_ERROR_NS.equals(namespace)) { return errorTypeRepository.lookupErrorType(errorIdentifier) .orElseThrow(() -> new MuleRuntimeException(createStaticMessage(format("There's no MULE error named '%s'.", identifier)))); } else if (errorTypeRepository.getErrorNamespaces().contains(namespace) && !syntheticErrorNamespaces.contains(namespace)) { throw new MuleRuntimeException(createStaticMessage(format("Cannot use error type '%s:%s': namespace already exists.", namespace, identifier))); } else if (syntheticErrorNamespaces.contains(namespace)) { Optional<ErrorType> optionalErrorType = errorTypeRepository.lookupErrorType(errorIdentifier); if (optionalErrorType.isPresent()) { return optionalErrorType.get(); } } else { syntheticErrorNamespaces.add(namespace); } return errorTypeRepository.addErrorType(errorIdentifier, errorTypeRepository.getAnyErrorType()); }
@Test public void doesNotAddInternalTypeAsRegularOne() { expectedException.expectMessage(is("An error type with identifier 'NS:NAME' already exists")); errorTypeRepository.addErrorType(INTERNAL_ERROR, errorTypeRepository.getAnyErrorType()); }
@Before public void setUp() { errorTypeRepository.addErrorType(CONNECTIVITY, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(ROUTING, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addErrorType(EXPOSED_ERROR, errorTypeRepository.getAnyErrorType()); errorTypeRepository.addInternalErrorType(INTERNAL_ERROR, errorTypeRepository.getCriticalErrorType()); errorTypeRepository.addInternalErrorType(OTHER_INTERNAL_ERROR, errorTypeRepository.getCriticalErrorType()); }
@Test public void matchChild() { ComponentIdentifier customTransformerIdentifier = ComponentIdentifier.builder().name("custom").namespace(CORE_NAMESPACE_NAME).build(); ErrorTypeRepository errorTypeRepository = muleContext.getErrorTypeRepository(); ErrorType customTransformerErrorType = errorTypeRepository.addErrorType(customTransformerIdentifier, transformationErrorType); ErrorTypeMatcher transformationMatcher = new SingleErrorTypeMatcher(transformationErrorType); assertThat(transformationMatcher.match(customTransformerErrorType), is(true)); }