private static boolean isRequestAbortedByClient(Exception exception) { return Throwables.getCausalChain(exception).stream().anyMatch(t -> t instanceof ClientAbortException); }
private static String getChainAsString(final Throwable th) { final List<Throwable> causalChain = Throwables.getCausalChain(th); final List<String> messages = Lists.transform(causalChain, new Function<Throwable, String>() { @Override public String apply(final Throwable input) { return input.toString(); } }); return Joiner.on(", ").join(messages); }
private RuntimeException handleException(RuntimeException t) { if (loggingConfig.isVerbose()) { return t; } for (Throwable y : Throwables.getCausalChain(t)) { if (y instanceof MessageException) { return (MessageException) y; } } return t; }
private static BadRequestException createBadRequestException(EmailException emailException) { List<String> messages = Throwables.getCausalChain(emailException) .stream() .map(Throwable::getMessage) .collect(Collectors.toList()); Collections.reverse(messages); return BadRequestException.create(messages); }
private static boolean shouldThrow(Exception e) { for (Throwable t : Throwables.getCausalChain(e)) { if (t instanceof HttpException) { HttpException http = (HttpException) t; return http.code() != HttpURLConnection.HTTP_NOT_FOUND; } if (t instanceof MessageException) { return true; } } return false; }
/** * Returns an optional with the first throwable in the causal chain that is assignable to the provided cause type, * and satisfies the provided cause predicate, {@link Optional#empty()} otherwise. * @param t The throwable to inspect for the cause. * @return */ private <T extends Throwable> Optional<T> findCause(Throwable t, Class<T> expectedCauseType, Predicate<T> causePredicate) { Stream<Throwable> causalChain = Throwables.getCausalChain(t).stream(); return causalChain .filter(expectedCauseType::isInstance) .map(expectedCauseType::cast) .filter(causePredicate) .findFirst(); }
public void testGetCasualChainNull() { try { Throwables.getCausalChain(null); fail("Should have throw NPE"); } catch (NullPointerException expected) { } }
public void testGetCausalChain() { SomeUncheckedException sue = new SomeUncheckedException(); IllegalArgumentException iae = new IllegalArgumentException(sue); RuntimeException re = new RuntimeException(iae); IllegalStateException ex = new IllegalStateException(re); assertEquals(asList(ex, re, iae, sue), Throwables.getCausalChain(ex)); assertSame(sue, Iterables.getOnlyElement(Throwables.getCausalChain(sue))); List<Throwable> causes = Throwables.getCausalChain(ex); try { causes.add(new RuntimeException()); fail("List should be unmodifiable"); } catch (UnsupportedOperationException expected) { } }
public void testGetCasualChainLoop() { Exception cause = new Exception(); Exception exception = new Exception(cause); cause.initCause(exception); try { Throwables.getCausalChain(cause); fail("Should have throw IAE"); } catch (IllegalArgumentException expected) { assertThat(expected).hasCauseThat().isSameAs(cause); } }
List<Throwable> causal = Throwables.getCausalChain(cause);
private boolean inspectIfAnyExceptionInChain(Throwable io, List<Class<? extends Exception>> ioList) { boolean exceptionToIgnore = false; for (Throwable t : Throwables.getCausalChain(io)) { for (Class<? extends Exception> exception : ioList) { exceptionToIgnore |= isExceptionInstanceOf(t, exception); } if (exceptionToIgnore) { break; } } return exceptionToIgnore; }
/** * Tests whether the exception itself, any of its causes, or any of their suppressed exceptions * matches the given predicate. * * @return true if a match was found, false otherwise * @throws IllegalArgumentException when there is a loop in the causal chain */ public static boolean anyInCausalChainOrSuppressedMatches( Throwable baseThrowable, Predicate<Throwable> predicate) { for (Throwable throwable : Throwables.getCausalChain(baseThrowable)) { if (predicate.test(throwable) || Arrays.stream(throwable.getSuppressed()).anyMatch(predicate)) { return true; } } return false; }
/** * Create an entry for the SYSTEM namespace in the SYSCAT table in case namespace mapping is enabled and system table * to system namespace mapping is also enabled. If not enabled, this method returns immediately without doing anything * @param metaConnection * @throws SQLException */ private void createSchemaIfNotExistsSystemNSMappingEnabled(PhoenixConnection metaConnection) throws SQLException { // HBase Namespace SYSTEM is assumed to be already created inside {@link ensureTableCreated(byte[], PTableType, // Map<String, Object>, List<Pair<byte[], Map<String, Object>>>, byte[][], boolean, boolean, boolean)}. // This statement will create an entry for the SYSTEM namespace in the SYSCAT table, so that GRANT/REVOKE // commands can work with SYSTEM Namespace. (See PHOENIX-4227 https://issues.apache.org/jira/browse/PHOENIX-4227) if (SchemaUtil.isNamespaceMappingEnabled(PTableType.SYSTEM, ConnectionQueryServicesImpl.this.getProps())) { try { metaConnection.createStatement().execute("CREATE SCHEMA IF NOT EXISTS " + PhoenixDatabaseMetaData.SYSTEM_CATALOG_SCHEMA); } catch (NewerSchemaAlreadyExistsException e) { // Older clients with appropriate perms may try getting a new connection // This results in NewerSchemaAlreadyExistsException, so we can safely ignore it here } catch (PhoenixIOException e) { if (!Iterables.isEmpty(Iterables.filter(Throwables.getCausalChain(e), AccessDeniedException.class))) { // Ignore ADE } else { throw e; } } } }
@Override public ConnectionValidationResult testConnection(Location location) { try { lazyComponentInitializer.initializeComponent(location); } catch (MuleRuntimeException e) { if (e.getCause() instanceof NoSuchComponentModelException) { throw new ObjectNotFoundException(location.toString()); } List<Throwable> causalChain = getCausalChain(e); return unknownFailureResponse(lastMessage(causalChain), e); } catch (Exception e) { return unknownFailureResponse(e.getCause().getMessage(), e); } return connectivityTestingService.testConnection(location); }
application = applicationSupplier.get(); } catch (Exception e) { throw getCausalChain(e).stream() .filter(exception -> exception.getClass().equals(ArtifactNotFoundException.class) || exception.getClass().equals(ArtifactResolutionException.class)) return failure(e.getMessage(), e); } catch (Exception e) { return getCausalChain(e).stream() .filter(exception -> exception.getClass().equals(ConnectionException.class) && ((ConnectionException) exception).getErrorType().isPresent())
@Override public boolean apply(final String input) { return any(getCausalChain(from), new Predicate<Throwable>() { @Override public boolean apply(Throwable arg0) { return (arg0.toString().indexOf(input) != -1) || (arg0.getMessage() != null && arg0.getMessage().indexOf(input) != -1); } }); }
@Override public boolean apply(final String input) { return any(getCausalChain(from), new Predicate<Throwable>() { @Override public boolean apply(Throwable arg0) { return (arg0.toString().indexOf(input) != -1) || (arg0.getMessage() != null && arg0.getMessage().indexOf(input) != -1); } }); }
@Override public boolean apply(final String input) { return any(getCausalChain(from), new Predicate<Throwable>() { @Override public boolean apply(Throwable arg0) { return (arg0.toString().indexOf(input) != -1) || (arg0.getMessage() != null && arg0.getMessage().indexOf(input) != -1); } }); }
for(Throwable throwable : Throwables.getCausalChain(ex)) { if(exception.equals(throwable.getClass())) { if(throwable instanceof AccessDeniedException) {
public static <T> T returnValueOnCodeOrNull(Throwable from, T value, Predicate<Integer> codePredicate) { Iterable<HttpResponseException> throwables = filter(getCausalChain(from), HttpResponseException.class); if (size(throwables) >= 1 && get(throwables, 0).getResponse() != null && codePredicate.apply(get(throwables, 0).getResponse().getStatusCode())) { return value; } return null; }