Search in sources :

Example 11 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class InternalExceptionUtils method getErrorFromFailingProcessor.

/**
 * Determine the {@link ErrorType} of a given exception thrown by a given message processor.
 *
 * @param processor the component that threw the exception (processor or source).
 * @param cause the exception thrown.
 * @param locator the {@link ErrorTypeLocator}.
 * @return the resolved {@link ErrorType}
 */
public static Error getErrorFromFailingProcessor(CoreEvent currentEvent, Component processor, Throwable cause, ErrorTypeLocator locator) {
    ErrorType currentError = currentEvent != null ? currentEvent.getError().map(Error::getErrorType).orElse(null) : null;
    ErrorType foundErrorType = locator.lookupErrorType(cause);
    ErrorType resultError = isUnknownMuleError(foundErrorType) ? currentError : foundErrorType;
    ErrorType errorType = getComponentIdentifier(processor).map(ci -> locator.lookupComponentErrorType(ci, cause)).orElse(locator.lookupErrorType(cause));
    return ErrorBuilder.builder(cause).errorType(getErrorMappings(processor).stream().filter(m -> m.match(resultError == null || isUnknownMuleError(resultError) ? errorType : currentError)).findFirst().map(ErrorMapping::getTarget).orElse(errorType)).build();
}
Also used : ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) Collections.emptyList(java.util.Collections.emptyList) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) Component(org.mule.runtime.api.component.Component) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ANNOTATION_ERROR_MAPPINGS(org.mule.runtime.core.internal.exception.ErrorMapping.ANNOTATION_ERROR_MAPPINGS) Error(org.mule.runtime.api.message.Error) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Error(org.mule.runtime.api.message.Error)

Example 12 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class MessagingExceptionResolver method resolve.

/**
 * Resolves a new {@link MessagingException} with the real cause of the problem based on the content of an Incoming
 * {@link MessagingException} with a chain of causes inside it and the current event that the exception is carrying.
 * <p>
 * This method will pick the FIRST cause exception that has a mule or extension KNOWN error as the real cause, if there is not
 * an exception in the causes that match with an Known error type then this method will try to find the error that the current
 * {@link Event} is carrying.
 * <p>
 * When there are multiple exceptions that contains the same root error type, then this method will wrap the one that has
 * highest position in the causes list
 *
 * @return a {@link MessagingException} with the proper {@link Error} associated to it's {@link CoreEvent}
 */
public MessagingException resolve(final MessagingException me, MuleContext context) {
    ErrorTypeLocator locator = ((PrivilegedMuleContext) context).getErrorTypeLocator();
    Optional<Pair<Throwable, ErrorType>> rootCause = findRoot(component, me, locator);
    if (!rootCause.isPresent()) {
        return updateCurrent(me, component, context);
    }
    Throwable root = rootCause.get().getFirst();
    ErrorType rootErrorType = rootCause.get().getSecond();
    Component failingComponent = getFailingProcessor(me, root).orElse(component);
    ErrorType errorType = getErrorMappings(component).stream().filter(m -> m.match(rootErrorType)).findFirst().map(ErrorMapping::getTarget).orElse(rootErrorType);
    Error error = ErrorBuilder.builder(getMessagingExceptionCause(root)).errorType(errorType).build();
    CoreEvent event = CoreEvent.builder(me.getEvent()).error(error).build();
    MessagingException result;
    if (root instanceof MessagingException) {
        ((MessagingException) root).setProcessedEvent(event);
        result = ((MessagingException) root);
    } else {
        result = me instanceof FlowExecutionException ? new FlowExecutionException(event, root, failingComponent) : new MessagingException(event, root, failingComponent);
    }
    if (me.getInfo().containsKey(INFO_ALREADY_LOGGED_KEY)) {
        result.addInfo(INFO_ALREADY_LOGGED_KEY, me.getInfo().get(INFO_ALREADY_LOGGED_KEY));
    }
    return enrich(result, failingComponent, event, context);
}
Also used : EnrichedNotificationInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo) ErrorMapping(org.mule.runtime.core.internal.exception.ErrorMapping) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) ExceptionUtils.getComponentIdentifier(org.mule.runtime.core.api.util.ExceptionUtils.getComponentIdentifier) InternalExceptionUtils.createErrorEvent(org.mule.runtime.core.internal.util.InternalExceptionUtils.createErrorEvent) ExceptionHelper.getExceptionsAsList(org.mule.runtime.api.exception.ExceptionHelper.getExceptionsAsList) Event(org.mule.runtime.api.event.Event) MuleContext(org.mule.runtime.core.api.MuleContext) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Component(org.mule.runtime.api.component.Component) ExceptionUtils.getMessagingExceptionCause(org.mule.runtime.core.api.util.ExceptionUtils.getMessagingExceptionCause) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) CRITICAL_IDENTIFIER(org.mule.runtime.core.api.exception.Errors.Identifiers.CRITICAL_IDENTIFIER) Pair(org.mule.runtime.api.util.Pair) LinkedList(java.util.LinkedList) Error(org.mule.runtime.api.message.Error) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) INFO_ALREADY_LOGGED_KEY(org.mule.runtime.api.exception.MuleException.INFO_ALREADY_LOGGED_KEY) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) EnrichedNotificationInfo.createInfo(org.mule.runtime.api.notification.EnrichedNotificationInfo.createInfo) ErrorBuilder(org.mule.runtime.core.internal.message.ErrorBuilder) List(java.util.List) InternalExceptionUtils.getErrorMappings(org.mule.runtime.core.internal.util.InternalExceptionUtils.getErrorMappings) Reference(org.mule.runtime.api.util.Reference) ErrorType(org.mule.runtime.api.message.ErrorType) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) Optional(java.util.Optional) CORE_NAMESPACE_NAME(org.mule.runtime.core.api.exception.Errors.CORE_NAMESPACE_NAME) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) ErrorType(org.mule.runtime.api.message.ErrorType) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Error(org.mule.runtime.api.message.Error) ExceptionUtils.isUnknownMuleError(org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) Component(org.mule.runtime.api.component.Component) Pair(org.mule.runtime.api.util.Pair)

Example 13 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class MessagingExceptionResolver method updateCurrent.

private MessagingException updateCurrent(MessagingException me, Component processor, MuleContext context) {
    CoreEvent errorEvent = createErrorEvent(me.getEvent(), processor, me, ((PrivilegedMuleContext) context).getErrorTypeLocator());
    Component failingProcessor = me.getFailingComponent() != null ? me.getFailingComponent() : processor;
    MessagingException updated = me instanceof FlowExecutionException ? new FlowExecutionException(errorEvent, me.getCause(), failingProcessor) : new MessagingException(me.getI18nMessage(), errorEvent, me.getCause(), failingProcessor);
    return enrich(updated, failingProcessor, errorEvent, context);
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowExecutionException(org.mule.runtime.core.internal.policy.FlowExecutionException) Component(org.mule.runtime.api.component.Component)

Example 14 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class AnnotatedObjectInvocationHandler method addAnnotationsToClass.

/**
 * Enhances the given {@code nonAnnotatedClass} to be an implementation of {@link Component}.
 *
 * @param clazz the {@link Class} to enhance to implement {@link Component}.
 * @return the enhanced class, or the given {@code clazz} if it was already annotated.
 * @throws UnsupportedOperationException if the given {@code clazz} is <b>not</b> annotated and is declared as {@code final}.
 */
public static <T, A extends Component> Class<A> addAnnotationsToClass(Class<T> clazz) {
    if (Component.class.isAssignableFrom(clazz) && asList(clazz.getMethods()).stream().anyMatch(m -> "getAnnotations".equals(m.getName()) && !m.isDefault())) {
        return (Class<A>) clazz;
    }
    if (isFinal(clazz.getModifiers())) {
        throw new UnsupportedOperationException("Class '" + clazz.getName() + "' must either not be final or implement '" + Component.class.getName() + "'");
    }
    Enhancer enhancer = new Enhancer();
    enhancer.setInterfaces(new Class[] { DynamicallyComponent.class });
    enhancer.setSuperclass(clazz);
    ComponentInterceptor annotatedObjectInvocationHandler = new ComponentInterceptor(MANAGED_METHODS);
    CallbackHelper callbackHelper = new CallbackHelper(clazz, new Class[] { DynamicallyComponent.class }) {

        @Override
        protected Object getCallback(Method method) {
            if (MANAGED_METHODS.contains(method) || annotatedObjectInvocationHandler.getOverridingMethods().containsKey(method)) {
                return annotatedObjectInvocationHandler;
            } else {
                Optional<Method> overridingMethod = MANAGED_METHODS.stream().filter(m -> m.getName().equals(method.getName()) && Arrays.equals(m.getParameterTypes(), method.getParameterTypes())).findFirst();
                if (overridingMethod.isPresent()) {
                    annotatedObjectInvocationHandler.getOverridingMethods().put(method, overridingMethod.get());
                    return annotatedObjectInvocationHandler;
                } else {
                    return NoOp.INSTANCE;
                }
            }
        }
    };
    enhancer.setCallbackTypes(callbackHelper.getCallbackTypes());
    enhancer.setCallbackFilter(callbackHelper);
    if (Enhancer.class.getClassLoader() != clazz.getClassLoader()) {
        enhancer.setClassLoader(new CompositeClassLoader(AnnotatedObjectInvocationHandler.class.getClassLoader(), clazz.getClassLoader()));
        enhancer.setUseCache(false);
    }
    Class<A> annotatedClass = enhancer.createClass();
    registerStaticCallbacks(annotatedClass, callbackHelper.getCallbacks());
    return annotatedClass;
}
Also used : Enhancer.registerStaticCallbacks(net.sf.cglib.proxy.Enhancer.registerStaticCallbacks) Arrays(java.util.Arrays) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader) Set(java.util.Set) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) HashMap(java.util.HashMap) Modifier.isStatic(java.lang.reflect.Modifier.isStatic) Field(java.lang.reflect.Field) DynamicallyComponent(org.mule.runtime.core.internal.component.DynamicallyComponent) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) HashSet(java.util.HashSet) Collections.unmodifiableSet(java.util.Collections.unmodifiableSet) Arrays.asList(java.util.Arrays.asList) NoOp(net.sf.cglib.proxy.NoOp) Collections.synchronizedMap(java.util.Collections.synchronizedMap) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) Modifier.isFinal(java.lang.reflect.Modifier.isFinal) Optional(java.util.Optional) Enhancer(net.sf.cglib.proxy.Enhancer) Method(java.lang.reflect.Method) MethodProxy(net.sf.cglib.proxy.MethodProxy) Enhancer(net.sf.cglib.proxy.Enhancer) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) Method(java.lang.reflect.Method) DynamicallyComponent(org.mule.runtime.core.internal.component.DynamicallyComponent) AbstractComponent(org.mule.runtime.api.component.AbstractComponent) Component(org.mule.runtime.api.component.Component) CompositeClassLoader(org.mule.runtime.core.internal.util.CompositeClassLoader)

Example 15 with Component

use of org.mule.runtime.api.component.Component in project mule by mulesoft.

the class LocatedMuleExceptionTestCase method namedAnnotatedComponent.

@Test
public void namedAnnotatedComponent() {
    Component namedAnnotated = mock(Component.class, withSettings().extraInterfaces(NamedObject.class));
    when(((NamedObject) namedAnnotated).getName()).thenReturn("mockComponent");
    when(namedAnnotated.getAnnotation(eq(docNameAttrName))).thenReturn("Mock Component");
    when(namedAnnotated.toString()).thenReturn("Mock@1");
    configureProcessorLocation(namedAnnotated);
    LocatedMuleException lme = new LocatedMuleException(namedAnnotated);
    assertThat(lme.getInfo().get(INFO_LOCATION_KEY).toString(), is("/mockComponent @ app:muleApp.xml:10 (Mock Component)"));
}
Also used : NamedObject(org.mule.runtime.api.meta.NamedObject) Component(org.mule.runtime.api.component.Component) LocatedMuleException(org.mule.runtime.api.exception.LocatedMuleException) Test(org.junit.Test) SmallTest(org.mule.tck.size.SmallTest)

Aggregations

Component (org.mule.runtime.api.component.Component)39 Test (org.junit.Test)15 Map (java.util.Map)12 AbstractComponent (org.mule.runtime.api.component.AbstractComponent)12 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)12 SmallTest (org.mule.tck.size.SmallTest)11 Optional (java.util.Optional)9 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)9 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 List (java.util.List)8 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)8 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)7 HashMap (java.util.HashMap)6 String.format (java.lang.String.format)5 Optional.empty (java.util.Optional.empty)5 Optional.of (java.util.Optional.of)5 Set (java.util.Set)5 Inject (javax.inject.Inject)5 Arrays.asList (java.util.Arrays.asList)4 Collection (java.util.Collection)4