@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())); }
if (errorTypeMatcher != null && !errorTypeMatcher.matches(error.getErrorType())) { matchers.add(errorTypeMatcher); if (causeMatcher != null && !causeMatcher.matches(error.getCause())) { matchers.add(causeMatcher); if (messageMatcher != null && !messageMatcher.matches(error.getDescription())) { matchers.add(messageMatcher); if (errorMessageMatcher != null && !errorMessageMatcher.matches(error.getErrorMessage())) { matchers.add(errorMessageMatcher);
private Optional<Error> mockError(ErrorType errorType, Throwable cause) { Error error = mock(Error.class); when(error.getErrorType()).thenReturn(errorType); when(error.getCause()).thenReturn(cause); return Optional.of(error); }
@Override protected boolean matchesSafely(CoreEvent item) { return (messageMatcher != null ? messageMatcher.matches(item.getMessage()) : true) && (variablesMatcher != null ? variablesMatcher.matches(item.getVariables()) : true) && (securityContextMatcher != null ? securityContextMatcher.matches(item.getSecurityContext()) : true) && (errorTypeMatcher != null ? errorTypeMatcher.matches(item.getError().get().getErrorType()) : true); }
/** * Creates an {@link Error} mock that will return the provided exception when calling {@link Error#getCause()} * * @param exception the exception to use to create the mock * @return a mocked {@link Error} */ public static Error createErrorMock(Exception exception) { Error errorMock = mock(Error.class); when(errorMock.getCause()).thenReturn(exception); return errorMock; }
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);
@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)); }
@Test public void childErrorsErrorBinding() throws Exception { String childErrorMessage = "error"; String otherChildErrorMessage = "oops"; ErrorType errorType = mock(ErrorType.class); Error error = mock(Error.class); when(error.getChildErrors()).thenReturn(asList( ErrorBuilder.builder(new IOException(childErrorMessage)).errorType(errorType) .build(), ErrorBuilder.builder(new DefaultMuleException(otherChildErrorMessage)) .errorType(errorType).build())); Optional opt = Optional.of(error); CoreEvent event = getEventWithError(opt); doReturn(testEvent().getMessage()).when(event).getMessage(); String expression = "error.childErrors reduce ((child, acc = '') -> acc ++ child.cause.message)"; TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build()); assertThat(result.getValue(), is(format("%s%s", childErrorMessage, otherChildErrorMessage))); }
@Override public Publisher<Void> onFailure(MessagingException exception, Map<String, Object> parameters) { final CoreEvent event = exception.getEvent(); final boolean isBackPressureError = event.getError() .map(e -> flowBackPressueErrorType.equals(e.getErrorType())) .orElse(false); SourceCallbackExecutor executor; if (isBackPressureError) { LOGGER.info("FLOW OVERLOAD - {}.", event.getError().get().getCause().getMessage()); executor = onBackPressureExecutor; parameters = emptyMap(); context.addVariable(BACK_PRESSURE_ACTION_CONTEXT_PARAM, backPressureAction); } else { executor = onErrorExecutor; } return from(executor.execute(event, parameters, context)).doAfterTerminate(this::rollback); }
@Test public void acceptsCriticalChild() { ErrorType errorType = mock(ErrorType.class); when(error.getErrorType()).thenReturn(errorType); when(errorType.getParentErrorType()).thenReturn(CRITICAL_ERROR_TYPE); assertThat(handler.accept(muleEvent), is(true)); }
@Override public CoreEvent process(CoreEvent event) throws MuleException { event.getError().ifPresent(theError -> exception = theError.getCause()); TypedValue<Object> payload = event.getMessage().getPayload(); if (payload.getValue() != null) { messages.add((TestTransactionalConnection) payload.getValue()); } return event; }
@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)); }
@Test public void childErrorsErrorBinding() throws Exception { String childErrorMessage = "error"; String otherChildErrorMessage = "oops"; ErrorType errorType = mock(ErrorType.class); Error error = mock(Error.class); when(error.getChildErrors()).thenReturn(asList( ErrorBuilder.builder(new IOException(childErrorMessage)).errorType(errorType) .build(), ErrorBuilder.builder(new DefaultMuleException(otherChildErrorMessage)) .errorType(errorType).build())); Optional opt = Optional.of(error); CoreEvent event = getEventWithError(opt); doReturn(testEvent().getMessage()).when(event).getMessage(); String expression = "error.childErrors reduce ((child, acc = '') -> acc ++ child.cause.message)"; TypedValue result = expressionLanguage.evaluate(expression, event, BindingContext.builder().build()); assertThat(result.getValue(), is(format("%s%s", childErrorMessage, otherChildErrorMessage))); }
@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())); }
@Test public void assignException() throws Exception { CoreEvent event = createEvent(); event = CoreEvent.builder(event).message(InternalMessage.builder(event.getMessage()).build()).build(); RuntimeException runtimeException = new RuntimeException(); when(mockError.getCause()).thenReturn(runtimeException); when(mockError.getErrorType()).thenReturn(mockErrorType); assertImmutableVariable("exception='other'", event); }
@Test public void acceptsCritical() { when(error.getErrorType()).thenReturn(CRITICAL_ERROR_TYPE); assertThat(handler.accept(muleEvent), is(true)); }
/** * Runs the specified flow with the provided event and configuration expecting a failure with a cause that matches the given * {@code matcher}. * <p> * Will fail if there's no failure running the flow. */ public void runExpectingException(Matcher<Throwable> causeMatcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getCause(), causeMatcher); } }
@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))); }
@Test public void exception() throws Exception { CoreEvent event = createEvent(); RuntimeException rte = new RuntimeException(); when(mockError.getCause()).thenReturn(rte); when(mockError.getErrorType()).thenReturn(mockErrorType); event = CoreEvent.builder(event).message(InternalMessage.builder(event.getMessage()).build()).build(); Object exception = evaluate("exception", event); assertThat(exception, is(instanceOf(MessagingException.class))); assertThat(((MessagingException) exception).getCause(), is(rte)); }
@Override protected boolean matchesSafely(MessagingException item) { errorTypeId = item.getEvent().getError().get().getErrorType().getIdentifier(); return FLOW_BACK_PRESSURE_ERROR_IDENTIFIER.equals(errorTypeId); } };