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