Search in sources :

Example 16 with Error

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

the class DataWeaveExpressionLanguageAdaptorTestCase method messageErrorBinding.

@Test
public void messageErrorBinding() throws Exception {
    Error error = mock(Error.class);
    when(error.getErrorMessage()).thenReturn(Message.of(new Integer[] { 1, 3, 6 }));
    Optional opt = Optional.of(error);
    CoreEvent event = getEventWithError(opt);
    doReturn(testEvent().getMessage()).when(event).getMessage();
    String expression = "error.errorMessage.payload reduce ($$ + $)";
    TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(10));
}
Also used : 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 17 with Error

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

the class DataWeaveExpressionLanguageAdaptorTestCase method errorBinding.

@Test
public void errorBinding() throws Exception {
    Error error = mock(Error.class);
    Optional opt = Optional.of(error);
    CoreEvent event = getEventWithError(opt);
    doReturn(testEvent().getMessage()).when(event).getMessage();
    TypedValue result = expressionLanguage.evaluate(ERROR, event, BindingContext.builder().build());
    assertThat(result.getValue(), is(sameInstance(error)));
}
Also used : Optional(java.util.Optional) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Error(org.mule.runtime.api.message.Error) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 18 with Error

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

the class ErrorHandlerTestCase method before.

@Before
public void before() throws MuleException {
    Error mockError = mock(Error.class);
    when(mockError.getErrorType()).thenReturn(mockErrorType);
    event = getEventBuilder().message(Message.of("")).error(mockError).build();
    mockException = new MessagingException(event, new Exception());
    CoreEvent handledEvent = testEvent();
    when(mockTestExceptionStrategy1.accept(any(CoreEvent.class))).thenReturn(true);
    when(mockTestExceptionStrategy1.apply(any(MessagingException.class))).thenReturn(just(handledEvent));
    when(mockTestExceptionStrategy2.accept(any(CoreEvent.class))).thenReturn(true);
    when(mockTestExceptionStrategy2.apply(any(MessagingException.class))).thenReturn(just(handledEvent));
}
Also used : CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Error(org.mule.runtime.api.message.Error) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before)

Example 19 with Error

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

the class CausedByFunction method call.

@Override
public Object call(Object[] parameters, BindingContext context) {
    Error error = (Error) parameters[0];
    checkArgument(error != null, "There's no error to match against.");
    String errorIdentifier = (String) parameters[1];
    ErrorTypeMatcher errorTypeMatcher = new SingleErrorTypeMatcher(resolveErrorType(errorIdentifier));
    return errorTypeMatcher.match(error.getErrorType());
}
Also used : ErrorTypeMatcher(org.mule.runtime.core.api.exception.ErrorTypeMatcher) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher) Error(org.mule.runtime.api.message.Error) SingleErrorTypeMatcher(org.mule.runtime.core.api.exception.SingleErrorTypeMatcher)

Example 20 with Error

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

the class LookupFunction method call.

@Override
public Object call(Object[] parameters, BindingContext context) {
    String flowName = (String) parameters[0];
    Object payload = parameters[1];
    Location componentLocation = Location.builder().globalName(flowName).build();
    Component component = componentLocator.find(componentLocation).orElseThrow(() -> new IllegalArgumentException(format("There is no component named '%s'.", flowName)));
    if (component instanceof Flow) {
        try {
            Message incomingMessage = lookupValue(context, MESSAGE, Message.builder().nullValue().build());
            Map<String, ?> incomingVariables = lookupValue(context, VARS, EMPTY_MAP);
            Error incomingError = lookupValue(context, ERROR, null);
            Message message = Message.builder(incomingMessage).value(payload).mediaType(APPLICATION_JAVA).build();
            CoreEvent event = CoreEvent.builder(PrivilegedEvent.getCurrentEvent().getContext()).variables(incomingVariables).error(incomingError).message(message).build();
            return ((ExecutableComponent) component).execute(event).get().getMessage().getPayload();
        } catch (ExecutionException e) {
            ComponentExecutionException componentExecutionException = (ComponentExecutionException) e.getCause();
            Error error = componentExecutionException.getEvent().getError().get();
            throw new MuleRuntimeException(createStaticMessage(format("Flow '%s' has failed with error '%s' (%s)", flowName, error.getErrorType(), error.getDescription())), error.getCause());
        } catch (InterruptedException e) {
            throw new MuleRuntimeException(e);
        }
    } else {
        throw new IllegalArgumentException(format("Component '%s' is not a flow.", flowName));
    }
}
Also used : Message(org.mule.runtime.api.message.Message) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Error(org.mule.runtime.api.message.Error) Flow(org.mule.runtime.core.api.construct.Flow) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Component(org.mule.runtime.api.component.Component) ExecutableComponent(org.mule.runtime.api.component.execution.ExecutableComponent) ExecutionException(java.util.concurrent.ExecutionException) ComponentExecutionException(org.mule.runtime.api.component.execution.ComponentExecutionException) Location(org.mule.runtime.api.component.location.Location)

Aggregations

Error (org.mule.runtime.api.message.Error)30 Test (org.junit.Test)18 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)11 SmallTest (org.mule.tck.size.SmallTest)11 MuleException (org.mule.runtime.api.exception.MuleException)10 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)9 ErrorType (org.mule.runtime.api.message.ErrorType)7 Optional (java.util.Optional)6 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ConnectionException (org.mule.runtime.api.connection.ConnectionException)5 MuleFatalException (org.mule.runtime.api.exception.MuleFatalException)5 TransformerException (org.mule.runtime.core.api.transformer.TransformerException)5 Component (org.mule.runtime.api.component.Component)4 TypedValue (org.mule.runtime.api.metadata.TypedValue)4 Message (org.mule.runtime.api.message.Message)3 ExceptionUtils.isUnknownMuleError (org.mule.runtime.core.api.util.ExceptionUtils.isUnknownMuleError)3 ErrorMapping (org.mule.runtime.core.internal.exception.ErrorMapping)3 List (java.util.List)2 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)2 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)2