use of org.mule.runtime.api.message.Error 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.message.Error in project mule by mulesoft.
the class ErrorBuilderTestCase method buildErrorFromException.
@Test
public void buildErrorFromException() {
RuntimeException exception = new RuntimeException(EXCEPTION_MESSAGE);
Error error = builder(exception).errorType(mockErrorType).build();
assertThat(error.getCause(), is(exception));
assertThat(error.getDescription(), is(EXCEPTION_MESSAGE));
assertThat(error.getDetailedDescription(), is(EXCEPTION_MESSAGE));
assertThat(error.getErrorType(), is(mockErrorType));
assertThat(error.getChildErrors(), is(empty()));
}
use of org.mule.runtime.api.message.Error in project mule by mulesoft.
the class ErrorBuilderTestCase method buildErrorFromCustomException.
@Test
public void buildErrorFromCustomException() {
Error error = builder(new ComposedErrorMessageAwareException(createStaticMessage(EXCEPTION_MESSAGE))).errorType(mockErrorType).build();
assertThat(error.getDescription(), is(EXCEPTION_MESSAGE));
assertThat(error.getDetailedDescription(), is(EXCEPTION_MESSAGE));
assertThat(error.getCause(), is(instanceOf(ComposedErrorMessageAwareException.class)));
assertThat(error.getErrorType(), is(mockErrorType));
assertThat(error.getErrorMessage().getPayload().getValue(), is(TEST_PAYLOAD));
List<Error> childErrors = error.getChildErrors();
assertThat(childErrors, hasSize(2));
assertThat(childErrors.get(0).getCause(), is(instanceOf(RuntimeException.class)));
assertThat(childErrors.get(1).getCause(), is(instanceOf(IOException.class)));
}
use of org.mule.runtime.api.message.Error in project mule by mulesoft.
the class ErrorBuilderTestCase method buildErrorFromMuleException.
@Test
public void buildErrorFromMuleException() {
MuleException exception = new DefaultMuleException(new RuntimeException(EXCEPTION_MESSAGE));
Error error = builder(exception).errorType(mockErrorType).build();
assertThat(error.getCause(), is(exception));
assertThat(error.getDescription(), containsString(EXCEPTION_MESSAGE));
assertThat(error.getDetailedDescription(), containsString(EXCEPTION_MESSAGE));
assertThat(error.getErrorType(), is(mockErrorType));
assertThat(error.getChildErrors(), is(empty()));
}
use of org.mule.runtime.api.message.Error in project mule by mulesoft.
the class HeisenbergMessageSourceTestCase method sourceFailsOnSuccessAndOnErrorParametersCallsOnTerminate.
@Test
public void sourceFailsOnSuccessAndOnErrorParametersCallsOnTerminate() throws Exception {
startFlow("sourceWithInvalidSuccessAndErrorParameters");
probe(TIMEOUT_MILLIS, POLL_DELAY_MILLIS, () -> assertState(false, false, true));
assertThat(HeisenbergSource.terminateStatus, is(ERROR_INVOKE));
Optional<Error> optionalError = HeisenbergSource.error;
assertThat(optionalError, is(not(empty())));
assertThat(optionalError.get().getErrorType(), is(errorType(SOURCE_ERROR_RESPONSE_GENERATE)));
MuleException me = (MuleException) unwrap(optionalError.get().getCause());
assertThat((String) me.getInfo().get(INFO_LOCATION_KEY), containsString("sourceWithInvalidSuccessAndErrorParameters/source"));
assertThat((String) me.getInfo().get(INFO_SOURCE_XML_KEY), containsString("heisenberg:success-info"));
}
Aggregations