@Override public Supplier<Error> resolve(ExecutionContext executionContext) { return () -> ((ExecutionContextAdapter) executionContext).getEvent().getError().orElse(null); } }
@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); }
@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; }
@Before public void setUp() { when(event.getError()).thenReturn(empty()); when(event.getAuthentication()).thenReturn(empty()); Message msg = of(null); when(event.getMessage()).thenReturn(msg); when(event.asBindingContext()).thenReturn(getTargetBindingContext(msg)); when(event.getItemSequenceInfo()).thenReturn(empty()); }
private CoreEvent getEventWithError(Optional<Error> error) { CoreEvent event = mock(CoreEvent.class, RETURNS_DEEP_STUBS); doReturn(error).when(event).getError(); when(event.getMessage().getPayload()).thenReturn(new TypedValue<>(null, OBJECT)); when(event.getMessage().getAttributes()).thenReturn(new TypedValue<>(null, OBJECT)); when(event.getAuthentication()).thenReturn(empty()); when(event.getItemSequenceInfo()).thenReturn(empty()); return event; }
private void assertExceptionErrorType(MessagingException me, ErrorType expected) { Optional<Error> error = me.getEvent().getError(); assertThat("No error found, expecting error with error type [" + expected + "]", error.isPresent(), is(true)); assertThat(error.get().getErrorType(), is(expected)); }
@Test public void payloadInfoConsumable() throws Exception { MuleException.verboseExceptions = true; CoreEvent testEvent = mock(CoreEvent.class); when(testEvent.getError()).thenReturn(empty()); final ByteArrayInputStream payload = new ByteArrayInputStream(new byte[] {}); Message muleMessage = of(payload); when(testEvent.getMessage()).thenReturn(muleMessage); MessagingException e = new MessagingException(createStaticMessage(message), testEvent); assertThat((String) e.getInfo().get(PAYLOAD_INFO_KEY), containsString(ByteArrayInputStream.class.getName() + "@")); verify(transformationService, never()).transform(muleMessage, DataType.STRING); }
@Override protected boolean matchesSafely(MessagingException item) { errorTypeId = item.getEvent().getError().get().getErrorType().getIdentifier(); return FLOW_BACK_PRESSURE_ERROR_IDENTIFIER.equals(errorTypeId); } };
@Test public void payloadInfoNonVerbose() throws Exception { MuleException.verboseExceptions = false; CoreEvent testEvent = mock(CoreEvent.class); Message muleMessage = spy(of("")); when(testEvent.getMessage()).thenReturn(muleMessage); when(testEvent.getError()).thenReturn(empty()); MessagingException e = new MessagingException(createStaticMessage(message), testEvent); assertThat(e.getInfo().get(PAYLOAD_INFO_KEY), nullValue()); verify(muleMessage, never()).getPayload(); verify(transformationService, never()).transform(muleMessage, DataType.STRING); }
/** * Runs the specified flow with the provided event and configuration expecting a failure with an error type that matches the * given {@code matcher}. * <p> * Will fail if there's no failure running the flow. */ public void runExpectingException(ErrorTypeMatcher matcher) throws Exception { try { runNoVerify(); fail("Flow executed successfully. Expecting exception"); } catch (EventProcessingException e) { verify(getFlowConstructName()); assertThat(e.getEvent().getError().get().getErrorType(), matcher); } }
/** * 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 resolveExceptionWithUnknownUnderlyingError() { Optional<Error> surfaceError = mockError(CONNECTION, null); when(event.getError()).thenReturn(surfaceError); MessagingException me = newMessagingException(new Exception(), event, processor); MessagingException resolved = resolver.resolve(me, context); assertExceptionErrorType(resolved, CONNECTION); }
@Test public void resolveExceptionWithCriticalUnderlyingError() { Optional<Error> surfaceError = mockError(CONNECTION, null); when(event.getError()).thenReturn(surfaceError); MessagingException me = newMessagingException(ERROR, event, processor); MessagingException resolved = resolver.resolve(me, context); assertExceptionErrorType(resolved, CONNECTION); }
@Test public void testHandleExceptionWithConfiguredMessageProcessors() throws Exception { onErrorContinueHandler .setMessageProcessors(asList(createSetStringMessageProcessor("A"), createSetStringMessageProcessor("B"))); initialiseIfNeeded(onErrorContinueHandler, muleContext); when(mockException.handled()).thenReturn(true); when(mockException.getDetailedMessage()).thenReturn(DEFAULT_LOG_MESSAGE); when(mockException.getEvent()).thenReturn(muleEvent); final CoreEvent result = onErrorContinueHandler.handleException(mockException, muleEvent); verify(mockException).setHandled(true); assertThat(result.getMessage().getPayload().getValue(), is("B")); assertThat(result.getError().isPresent(), is(false)); }
@Test public void resolveWithAnEventThatCarriesError() { Optional<Error> surfaceError = mockError(TRANSFORMER, null); when(event.getError()).thenReturn(surfaceError); MessagingException me = newMessagingException(new Exception(), event, processor); MessagingException resolved = resolver.resolve(me, context); assertExceptionErrorType(resolved, TRANSFORMER); assertExceptionMessage(resolved.getMessage(), ERROR_MESSAGE); }
@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); }
@Test public void resolveWithMultipleErrors() { Optional<Error> surfaceError = mockError(TRANSFORMER, TRANSFORMER_EXCEPTION); when(event.getError()).thenReturn(surfaceError); Exception cause = new Exception(new ConnectionException(FATAL_EXCEPTION)); MessagingException me = newMessagingException(cause, event, processor); MessagingException resolved = resolver.resolve(me, context); assertExceptionErrorType(resolved, FATAL); assertExceptionMessage(resolved.getMessage(), FATAL_EXCEPTION.getMessage()); }
@Test public void resolveTopExceptionWithSameError() { Optional<Error> surfaceError = mockError(TRANSFORMER, TRANSFORMER_EXCEPTION); when(event.getError()).thenReturn(surfaceError); Exception cause = new MuleFatalException(createStaticMessage(EXPECTED_MESSAGE), new LinkageError("!")); MessagingException me = newMessagingException(cause, event, processor); MessagingException resolved = resolver.resolve(me, context); assertExceptionErrorType(resolved, FATAL); assertExceptionMessage(resolved.getMessage(), EXPECTED_MESSAGE); }
private String getPayload(Processor mp, MuleSession session, String message) throws Exception { Message msg = of(message); try { CoreEvent event = mp.process(this.<PrivilegedEvent.Builder>getEventBuilder().message(msg).session(session).build()); Message returnedMessage = event.getMessage(); if (event.getError().isPresent()) { return EXCEPTION_SEEN; } else { return getPayloadAsString(returnedMessage); } } catch (Exception ex) { return EXCEPTION_SEEN; } }