Search in sources :

Example 6 with ErrorTypeRepository

use of org.mule.runtime.api.exception.ErrorTypeRepository in project mule by mulesoft.

the class ThrowProcessor method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    if (count == -1 || count-- > 0) {
        try {
            Throwable instantiatedException = exception.newInstance();
            if (error != null) {
                ErrorTypeRepository errorTypeRepository = muleContext.getErrorTypeRepository();
                ErrorType errorType = errorTypeRepository.lookupErrorType(buildFromStringRepresentation(error)).orElseThrow(() -> new DefaultMuleException(format("Could not find error: '%s'", error)));
                throw new TypedException(instantiatedException, errorType);
            } else {
                checkArgument(instantiatedException instanceof TypedException, EXCEPTION_ERROR);
                throw (TypedException) instantiatedException;
            }
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DefaultMuleException(format("Failed to instantiate exception class '%s'", exception.getSimpleName()));
        }
    } else {
        return event;
    }
}
Also used : ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) ErrorType(org.mule.runtime.api.message.ErrorType) TypedException(org.mule.runtime.api.exception.TypedException)

Example 7 with ErrorTypeRepository

use of org.mule.runtime.api.exception.ErrorTypeRepository in project mule by mulesoft.

the class MuleContextUtils method mockContextWithServices.

/**
 * Creates and configures a mock {@link MuleContext} to return testing services implementations.
 *
 * @return the created {@code muleContext}.
 */
public static MuleContextWithRegistries mockContextWithServices() {
    final MuleContextWithRegistries muleContext = mockMuleContext();
    SchedulerService schedulerService = spy(new SimpleUnitTestSupportSchedulerService());
    when(muleContext.getSchedulerService()).thenReturn(schedulerService);
    ErrorTypeRepository errorTypeRepository = mock(ErrorTypeRepository.class);
    when(muleContext.getErrorTypeRepository()).thenReturn(errorTypeRepository);
    when(errorTypeRepository.getErrorType(any(ComponentIdentifier.class))).thenReturn(of(mock(ErrorType.class)));
    final MuleRegistry registry = muleContext.getRegistry();
    NotificationListenerRegistry notificationListenerRegistry = mock(NotificationListenerRegistry.class);
    ConfigurationProperties configProps = mock(ConfigurationProperties.class);
    when(configProps.resolveBooleanProperty(any())).thenReturn(empty());
    ConfigurationComponentLocator configurationComponentLocator = mock(ConfigurationComponentLocator.class);
    when(configurationComponentLocator.find(any(Location.class))).thenReturn(empty());
    when(configurationComponentLocator.find(any(ComponentIdentifier.class))).thenReturn(emptyList());
    try {
        when(registry.lookupObject(NotificationListenerRegistry.class)).thenReturn(notificationListenerRegistry);
        Map<Class, Object> injectableObjects = new HashMap<>();
        injectableObjects.put(MuleContext.class, muleContext);
        injectableObjects.put(SchedulerService.class, schedulerService);
        injectableObjects.put(ErrorTypeRepository.class, errorTypeRepository);
        injectableObjects.put(ExtendedExpressionManager.class, muleContext.getExpressionManager());
        injectableObjects.put(StreamingManager.class, muleContext.getRegistry().lookupObject(StreamingManager.class));
        injectableObjects.put(ObjectStoreManager.class, muleContext.getRegistry().lookupObject(OBJECT_STORE_MANAGER));
        injectableObjects.put(NotificationDispatcher.class, muleContext.getRegistry().lookupObject(NotificationDispatcher.class));
        injectableObjects.put(NotificationListenerRegistry.class, notificationListenerRegistry);
        injectableObjects.put(ConfigurationComponentLocator.class, configurationComponentLocator);
        injectableObjects.put(ConfigurationProperties.class, configProps);
        // Ensure injection of consistent mock objects
        when(muleContext.getInjector()).thenReturn(new MocksInjector(injectableObjects));
    } catch (RegistrationException e1) {
        throw new MuleRuntimeException(e1);
    }
    return muleContext;
}
Also used : RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) SchedulerService(org.mule.runtime.api.scheduler.SchedulerService) HashMap(java.util.HashMap) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ConfigurationProperties(org.mule.runtime.api.component.ConfigurationProperties) ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) StreamingManager(org.mule.runtime.core.api.streaming.StreamingManager) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) ConfigurationComponentLocator(org.mule.runtime.api.component.location.ConfigurationComponentLocator) SimpleUnitTestSupportSchedulerService(org.mule.tck.SimpleUnitTestSupportSchedulerService) Location(org.mule.runtime.api.component.location.Location)

Example 8 with ErrorTypeRepository

use of org.mule.runtime.api.exception.ErrorTypeRepository in project mule by mulesoft.

the class ErrorTypeRepositoryFactory method createDefaultErrorTypeRepository.

/**
 * Creates the default {@link ErrorTypeRepository} to use in mule.
 * <p>
 * The {@link ErrorTypeRepository} gets populated with the default mappings between common core exceptions and core error types.
 *
 * @return a new {@link ErrorTypeRepository}.
 */
public static ErrorTypeRepository createDefaultErrorTypeRepository() {
    ErrorTypeRepository errorTypeRepository = new DefaultErrorTypeRepository();
    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.addInternalErrorType(OVERLOAD, errorTypeRepository.getCriticalErrorType());
    errorTypeRepository.addInternalErrorType(FLOW_BACK_PRESSURE, errorTypeRepository.getErrorType(OVERLOAD).get());
    errorTypeRepository.addInternalErrorType(FATAL, errorTypeRepository.getCriticalErrorType());
    errorTypeRepository.addErrorType(TIMEOUT, errorTypeRepository.getAnyErrorType());
    errorTypeRepository.addErrorType(COMPOSITE_ROUTING, errorTypeRepository.getErrorType(ROUTING).get());
    final ErrorType sourceErrorType = errorTypeRepository.getSourceErrorType();
    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);
    return errorTypeRepository;
}
Also used : ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) ErrorType(org.mule.runtime.api.message.ErrorType)

Example 9 with ErrorTypeRepository

use of org.mule.runtime.api.exception.ErrorTypeRepository in project mule by mulesoft.

the class OnErrorContinueHandler method doInitialise.

@Override
protected void doInitialise(MuleContext muleContext) throws InitialisationException {
    super.doInitialise(muleContext);
    ErrorTypeRepository errorTypeRepository = muleContext.getErrorTypeRepository();
    sourceErrorMatcher = new SingleErrorTypeMatcher(errorTypeRepository.getSourceResponseErrorType());
    if (errorType != null) {
        String[] errors = errorType.split(",");
        for (String error : errors) {
            // Since the partial initialisation was successful, we know this error ids are safe
            String sanitizedError = error.trim();
            ErrorType errorType = errorTypeRepository.lookupErrorType(buildFromStringRepresentation(sanitizedError)).get();
            if (sourceErrorMatcher.match(errorType)) {
                throw new InitialisationException(getInitialisationError(sanitizedError), this);
            }
        }
    } else if (when == null) {
        // No error type and no expression, force ANY matcher
        errorTypeMatcher = new SingleErrorTypeMatcher(errorTypeRepository.getAnyErrorType());
    }
}
Also used : ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) ErrorType(org.mule.runtime.api.message.ErrorType) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher)

Example 10 with ErrorTypeRepository

use of org.mule.runtime.api.exception.ErrorTypeRepository in project mule by mulesoft.

the class ExtensionPluginMetadataGenerator method createExtensionManager.

/**
 * Creates a {@link ExtensionManager} needed for generating the metadata for an extension. It would be later discarded due to
 * the manager would have references to classes loaded with the launcher class loader instead of the hierarchical class loaders
 * created as result of the classification process.
 *
 * @return an {@link ExtensionManager} that would be used to register the extensions.
 */
private ExtensionManager createExtensionManager() {
    DefaultExtensionManager extensionManager = new DefaultExtensionManager();
    extensionManager.setMuleContext(new DefaultMuleContext() {

        private ErrorTypeRepository errorTypeRepository = createDefaultErrorTypeRepository();

        private ErrorTypeLocator errorTypeLocator = createDefaultErrorTypeLocator(errorTypeRepository);

        @Override
        public MuleRegistry getRegistry() {
            return new MuleRegistryHelper(new DefaultRegistryBroker(this, new MuleLifecycleInterceptor()), this);
        }

        @Override
        public ErrorTypeLocator getErrorTypeLocator() {
            return errorTypeLocator;
        }

        @Override
        public ErrorTypeRepository getErrorTypeRepository() {
            return errorTypeRepository;
        }
    });
    try {
        extensionManager.initialise();
    } catch (InitialisationException e) {
        throw new RuntimeException("Error while initialising the extension manager", e);
    }
    return extensionManager;
}
Also used : ErrorTypeRepository(org.mule.runtime.api.exception.ErrorTypeRepository) ErrorTypeRepositoryFactory.createDefaultErrorTypeRepository(org.mule.runtime.core.internal.exception.ErrorTypeRepositoryFactory.createDefaultErrorTypeRepository) MuleRegistryHelper(org.mule.runtime.core.internal.registry.MuleRegistryHelper) DefaultMuleContext(org.mule.runtime.core.internal.context.DefaultMuleContext) DefaultRegistryBroker(org.mule.runtime.core.internal.registry.DefaultRegistryBroker) ErrorTypeLocatorFactory.createDefaultErrorTypeLocator(org.mule.runtime.core.internal.exception.ErrorTypeLocatorFactory.createDefaultErrorTypeLocator) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) MuleRegistry(org.mule.runtime.core.internal.registry.MuleRegistry) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleLifecycleInterceptor(org.mule.runtime.core.internal.lifecycle.MuleLifecycleInterceptor) DefaultExtensionManager(org.mule.runtime.module.extension.internal.manager.DefaultExtensionManager)

Aggregations

ErrorTypeRepository (org.mule.runtime.api.exception.ErrorTypeRepository)11 ErrorType (org.mule.runtime.api.message.ErrorType)4 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)3 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)3 SingleErrorTypeMatcher (org.mule.runtime.core.api.exception.SingleErrorTypeMatcher)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 ConfigurationComponentLocator (org.mule.runtime.api.component.location.ConfigurationComponentLocator)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 ErrorTypeMatcher (org.mule.runtime.core.api.exception.ErrorTypeMatcher)2 TRUE (java.lang.Boolean.TRUE)1 ArrayList (java.util.ArrayList)1 Arrays.stream (java.util.Arrays.stream)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1