@Test public void extractExceptionOfSubtype() { Exception exception = new Exception(new IllegalStateException(new java.lang.Error(new RuntimeException(new IOException())))); Optional<RuntimeException> runtimeException = extractOfType(exception, RuntimeException.class); assertThat(runtimeException.isPresent(), is(true)); assertThat(runtimeException.get(), instanceOf(IllegalStateException.class)); }
private void logDeploymentFailure(Throwable t, String artifactName) { if (containsType(t, DeploymentStartException.class)) { log(miniSplash(format("Failed to deploy artifact '%s', see artifact's log for details", artifactName))); logger.error(t.getMessage()); } else { log(miniSplash(format("Failed to deploy artifact '%s', see below", artifactName))); logger.error(t.getMessage(), t); } }
/** * Given a {@link Throwable} instance this method will get the specific failure reason. * <p> * If there is a {@link ConnectionException} in the stacktrace is going to be considered the main failure reason, * otherwise it will check if there is a {@link UndeclaredThrowableException} reflective wrapper exception * in the stacktrace wrapping the real failure. */ public Throwable handleThrowable(Throwable e) { Optional<ConnectionException> connectionException = extractConnectionException(e); if (connectionException.isPresent()) { return connectionException.get(); } else { // unwraps the exception thrown by the reflective operation if exist any. return extractCauseOfType(e, UndeclaredThrowableException.class).orElse(e); } }
@Override public Throwable onError(ExecutionContext<ComponentModel> executionContext, Throwable exception) { extractConnectionException(exception).ifPresent( e -> setCloseCommand(executionContext, () -> withConnection(executionContext, ConnectionHandler::invalidate))); return exception; }
private AccessTokenExpiredException getTokenExpirationException(Exception e) { return e instanceof AccessTokenExpiredException ? (AccessTokenExpiredException) e : (AccessTokenExpiredException) extractCauseOfType(e, AccessTokenExpiredException.class).orElse(null); }
/** * 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(); } }
@Test public void testFullStackTraceWithoutMessage() throws Exception { final String mainMessage = "main message 112312 [][] ''' ... sdfsd blah"; final String causeMessage = "cause message 2342998n fwefoskjdcas sdcasdhfsadjgsadkgasd \t" + lineSeparator() + "sdfsllki"; Exception e = new RuntimeException(mainMessage, new RuntimeException(causeMessage)); String withoutMessage = getFullStackTraceWithoutMessages(e); String fullStackTrace = getStackTrace(e); String[] linesWithoutMessage = withoutMessage.split(lineSeparator()); String[] lines = fullStackTrace.split(lineSeparator()); assertEquals(lines.length, linesWithoutMessage.length); for (int i = 0; i < lines.length; i++) { assertTrue(lines[i].contains(linesWithoutMessage[i])); assertFalse(linesWithoutMessage[i].contains(mainMessage)); assertFalse(linesWithoutMessage[i].contains(causeMessage)); } }
private Function<Throwable, Throwable> getThrowableFunction(CoreEvent event) { return throwable -> { Throwable cause = getMessagingExceptionCause(throwable); return new MessagingException(event, new RetryPolicyExhaustedException(createStaticMessage(UNTIL_SUCCESSFUL_MSG_PREFIX, cause.getMessage()), cause, this), this); }; }
private ErrorType errorTypeFromException(Component failing, ErrorTypeLocator locator, Throwable e) { if (isMessagingExceptionWithError(e)) { return ((MessagingException) e).getEvent().getError().map(Error::getErrorType).orElse(locator.lookupErrorType(e)); } else { Optional<ComponentIdentifier> componentIdentifier = getComponentIdentifier(failing); return componentIdentifier.map(ci -> locator.lookupComponentErrorType(ci, e)).orElse(locator.lookupErrorType(e)); } }
/** * Retrieves the connection for the related component and configuration * * @param <C> Connection type * @return A connection instance of {@param <C>} type for the component. If the related configuration does not require a * connection {@link Optional#empty()} will be returned * @throws ConnectionException when no valid connection is found for the related component and configuration */ public <C> Optional<C> getConnection() throws ConnectionException { try { return (Optional<C>) connectionHandler.get() .map((CheckedFunction<ConnectionHandler, Object>) ConnectionHandler::getConnection); } catch (Exception e) { Optional<ConnectionException> connectionException = extractConnectionException(e); throw connectionException.orElse(new ConnectionException(e)); } }
@Test public void extractExceptionCauseOf() { Exception exception = new Exception(new IOException(new ConnectionException(ERROR_MESSAGE, new NullPointerException()))); Optional<? extends Throwable> throwable = extractCauseOfType(exception, IOException.class); assertThat(throwable.isPresent(), is(true)); assertThat(throwable.get(), instanceOf(ConnectionException.class)); assertThat(throwable.get().getMessage(), is(ERROR_MESSAGE)); }
@Test public void testFullStackTraceWithoutMessage() throws Exception { final String mainMessage = "main message 112312 [][] ''' ... sdfsd blah"; final String causeMessage = "cause message 2342998n fwefoskjdcas sdcasdhfsadjgsadkgasd \t" + lineSeparator() + "sdfsllki"; Exception e = new RuntimeException(mainMessage, new RuntimeException(causeMessage)); String withoutMessage = getFullStackTraceWithoutMessages(e); String fullStackTrace = getStackTrace(e); String[] linesWithoutMessage = withoutMessage.split(lineSeparator()); String[] lines = fullStackTrace.split(lineSeparator()); assertEquals(lines.length, linesWithoutMessage.length); for (int i = 0; i < lines.length; i++) { assertTrue(lines[i].contains(linesWithoutMessage[i])); assertFalse(linesWithoutMessage[i].contains(mainMessage)); assertFalse(linesWithoutMessage[i].contains(causeMessage)); } }
.orElse(rootErrorType); Error error = ErrorBuilder.builder(getMessagingExceptionCause(root)).errorType(errorType).build(); CoreEvent event = CoreEvent.builder(me.getEvent()).error(error).build();
@Override public boolean matches(Object item) { ConfigurationException configurationException = extractOfType((Throwable) item, ConfigurationException.class).get(); assertThat(configurationException.getMessage(), equalTo("Element <heisenberg:secure-connection> in line 17 of file " + "heisenberg-missing-tls-connection-config.xml is missing " + "required parameter 'tls-context'")); return true; }
@Test public void testContainsType() { assertTrue(containsType(new IllegalArgumentException(), IllegalArgumentException.class)); assertTrue(containsType(new Exception(new IllegalArgumentException()), IllegalArgumentException.class)); assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), NullPointerException.class)); assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), RuntimeException.class)); assertTrue(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), Exception.class)); assertFalse(containsType(new Exception(new IllegalArgumentException(new NullPointerException())), IOException.class)); }
@Override public Throwable onError(ExecutionContext<OperationModel> executionContext, Throwable exception) { extractConnectionException(exception).ifPresent(cne -> { Map<Cursor, Long> cursorPositions = ((ExecutionContextAdapter<OperationModel>) executionContext).removeVariable(CURSOR_POSITIONS); if (cursorPositions != null) { cursorPositions.forEach((cursor, position) -> { try { cursor.seek(position); } catch (IOException e) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Could not reset cursor back to position " + position + ". Inconsistencies might occur if " + "reconnection attempted", e); } } }); } }); return exception; } }
/** * Given a {@link Throwable} instance this method will get the specific failure reason. * <p> * If there is a {@link ConnectionException} in the stacktrace is going to be considered the main failure reason, * otherwise it will check if there is a {@link UndeclaredThrowableException} reflective wrapper exception * in the stacktrace wrapping the real failure. */ public Throwable handleThrowable(Throwable e) { Optional<ConnectionException> connectionException = extractConnectionException(e); if (connectionException.isPresent()) { return connectionException.get(); } else { // unwraps the exception thrown by the reflective operation if exist any. return extractCauseOfType(e, UndeclaredThrowableException.class).orElse(e); } }
private AccessTokenExpiredException getTokenExpirationException(Exception e) { return e instanceof AccessTokenExpiredException ? (AccessTokenExpiredException) e : (AccessTokenExpiredException) extractCauseOfType(e, AccessTokenExpiredException.class).orElse(null); }
@Test public void extractExceptionOfType() { Exception exception = new Exception(new Throwable(new ConnectionException(new IOException(new NullPointerException())))); Optional<IOException> ioException = extractOfType(exception, IOException.class); assertThat(ioException.isPresent(), is(true)); assertThat(ioException.get().getCause(), instanceOf(NullPointerException.class)); }
if (containsType(t, DeploymentStartException.class)) { log(miniSplash(format("Failed to deploy artifact '%s', see artifact's log for details", addedApp))); logger.error(t.getMessage());