Search in sources :

Example 1 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class DataWeaveExpressionLanguageAdaptorTestCase method fullErrorBinding.

@Test
public void fullErrorBinding() throws Exception {
    String description = "An error occurred";
    String detailedDescription = "A division by zero has collapsed our systems.";
    String exceptionMessage = "dividend cannot be zero";
    String errorId = "WEAVE_TEST";
    ErrorType errorType = mock(ErrorType.class);
    when(errorType.getIdentifier()).thenReturn(errorId);
    Error error = ErrorBuilder.builder().description(description).detailedDescription(detailedDescription).exception(new IllegalArgumentException(exceptionMessage)).errorType(errorType).build();
    Optional opt = Optional.of(error);
    CoreEvent event = getEventWithError(opt);
    doReturn(testEvent().getMessage()).when(event).getMessage();
    String expression = "'$(error.description) $(error.detailedDescription) $(error.cause.message) $(error.errorType.identifier)'";
    TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(format("%s %s %s %s", description, detailedDescription, exceptionMessage, errorId)));
}
Also used : ErrorType(org.mule.runtime.api.message.ErrorType) Optional(java.util.Optional) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Error(org.mule.runtime.api.message.Error) Matchers.containsString(org.hamcrest.Matchers.containsString) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 2 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class MessagingExceptionResolverTestCase method resolveCorrectConnectionException.

@Test
public void resolveCorrectConnectionException() {
    ErrorType expected = ErrorTypeBuilder.builder().namespace("NS").identifier("CONNECTION").parentErrorType(CONNECTION).build();
    ErrorTypeLocator locator = ErrorTypeLocator.builder(repository).addComponentExceptionMapper(ci, ExceptionMapper.builder().addExceptionMapping(ConnectionException.class, expected).build()).defaultExceptionMapper(ExceptionMapper.builder().build()).defaultError(UNKNOWN).build();
    when(((PrivilegedMuleContext) context).getErrorTypeLocator()).thenReturn(locator);
    MessagingException me = newMessagingException(CONNECTION_EXCEPTION, event, processor);
    MessagingExceptionResolver anotherResolver = new MessagingExceptionResolver(new TestProcessor());
    MessagingException resolved = anotherResolver.resolve(me, context);
    assertExceptionErrorType(resolved, expected);
    assertExceptionMessage(resolved.getMessage(), "CONNECTION PROBLEM");
}
Also used : ErrorType(org.mule.runtime.api.message.ErrorType) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ErrorTypeLocator(org.mule.runtime.core.privileged.exception.ErrorTypeLocator) MessagingExceptionResolver(org.mule.runtime.core.internal.util.MessagingExceptionResolver) PrivilegedMuleContext(org.mule.runtime.core.privileged.PrivilegedMuleContext) ConnectionException(org.mule.runtime.api.connection.ConnectionException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 3 with ErrorType

use of org.mule.runtime.api.message.ErrorType in project mule by mulesoft.

the class MessagingExceptionResolverTestCase method resolveWithParentInChain.

@Test
public void resolveWithParentInChain() {
    ErrorType withParent = ErrorTypeBuilder.builder().parentErrorType(CONNECTION).identifier("CONNECT").namespace("TEST").build();
    Optional<Error> surfaceError = mockError(withParent, new Exception());
    when(event.getError()).thenReturn(surfaceError);
    Exception cause = new ConnectionException("Some Connection Error", new Exception());
    MessagingException me = newMessagingException(cause, event, processor);
    MessagingException resolved = resolver.resolve(me, context);
    assertExceptionErrorType(resolved, withParent);
    assertExceptionMessage(resolved.getMessage(), ERROR_MESSAGE);
}
Also used : ErrorType(org.mule.runtime.api.message.ErrorType) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Error(org.mule.runtime.api.message.Error) MuleFatalException(org.mule.runtime.api.exception.MuleFatalException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) TransformerException(org.mule.runtime.core.api.transformer.TransformerException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) ConnectionException(org.mule.runtime.api.connection.ConnectionException) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 4 with ErrorType

use of org.mule.runtime.api.message.ErrorType 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 5 with ErrorType

use of org.mule.runtime.api.message.ErrorType 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)

Aggregations

ErrorType (org.mule.runtime.api.message.ErrorType)35 Test (org.junit.Test)19 SmallTest (org.mule.tck.size.SmallTest)9 SingleErrorTypeMatcher (org.mule.runtime.core.api.exception.SingleErrorTypeMatcher)8 Error (org.mule.runtime.api.message.Error)7 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)6 ErrorTypeMatcher (org.mule.runtime.core.api.exception.ErrorTypeMatcher)6 ErrorTypeLocator (org.mule.runtime.core.privileged.exception.ErrorTypeLocator)6 ErrorTypeRepository (org.mule.runtime.api.exception.ErrorTypeRepository)5 Optional (java.util.Optional)4 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)4 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)4 PrivilegedMuleContext (org.mule.runtime.core.privileged.PrivilegedMuleContext)4 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Component (org.mule.runtime.api.component.Component)3 TypedException (org.mule.runtime.api.exception.TypedException)3 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)3 HashSet (java.util.HashSet)2