private static <T extends Throwable> T copyContents(final Exception source, final T throwable) { throwable.setStackTrace(source.getStackTrace()); final Throwable[] suppressed = source.getSuppressed(); if (suppressed != null) for (final Throwable t : suppressed) { throwable.addSuppressed(t); } return throwable; } }
@Override public boolean matches( Object item ) { Exception e = (Exception) item; for ( Throwable suppressed : e.getSuppressed() ) { if ( suppressed instanceof ResourceCloseFailureException ) { if ( suppressed.getCause() instanceof ExceptionDuringClose ) { return true; } } } return false; } } );
@Test public void log_error_as_suppressed_when_task_failed_with_MessageException_and_ending_state_can_not_be_persisted_to_db() throws Exception { CeTask ceTask = createCeTask(submitter); when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); MessageException ex = makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process")); RuntimeException runtimeException = new RuntimeException("Simulate queue#remove failing"); doThrow(runtimeException).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); underTest.call(); List<String> logs = logTester.logs(LoggerLevel.INFO); assertThat(logs).hasSize(2); assertThat(logs.get(0)).contains(" | submitter=" + submitter.getLogin()); assertThat(logs.get(1)).contains(String.format(" | submitter=%s | status=FAILED | time=", submitter.getLogin())); List<LogAndArguments> logAndArguments = logTester.getLogs(LoggerLevel.ERROR); assertThat(logAndArguments).hasSize(1); assertThat(logAndArguments.get(0).getFormattedMsg()).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); Object arg1 = logAndArguments.get(0).getArgs().get()[0]; assertThat(arg1).isSameAs(runtimeException); assertThat(((Exception) arg1).getSuppressed()).containsOnly(ex); }
@Test public void testTrace2() throws Exception { Hooks.onOperatorDebug(); try { Mono.just(1) .map(d -> { throw new RuntimeException(); }) .filter(d -> true) .doOnNext(d -> System.currentTimeMillis()) .map(d -> d) .block(); } catch(Exception e){ e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains ("HooksTraceTest.java:")); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains("|_\tMono.map ⇢ reactor.HooksTraceTest.testTrace2(HooksTraceTest.java:")); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
@Test public void testTraceComposed() throws Exception { Hooks.onOperatorDebug(); try { Mono.just(1) .flatMap(d -> Mono.error(new RuntimeException())) .filter(d -> true) .doOnNext(d -> System.currentTimeMillis()) .map(d -> d) .block(); } catch (Exception e) { e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage() .contains("HooksTraceTest.java:")); Assert.assertTrue(e.getSuppressed()[0].getMessage() .contains("|_\tMono.flatMap ⇢ reactor.HooksTraceTest.testTraceComposed(HooksTraceTest.java:")); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
@Test public void testTrace() throws Exception { Hooks.onOperatorDebug(); try { Mono.fromCallable(() -> { throw new RuntimeException(); }) .map(d -> d) .block(); } catch(Exception e){ e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains("MonoCallable")); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
@Test public void testTraceDefer() throws Exception { Hooks.onOperatorDebug(); try { Mono.defer(() -> Mono.just(1) .flatMap(d -> Mono.error(new RuntimeException())) .filter(d -> true) .doOnNext(d -> System.currentTimeMillis()) .map(d -> d)) .block(); } catch(Exception e){ e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains ("HooksTraceTest.java:")); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains("|_\tMono.flatMap ⇢ reactor.HooksTraceTest.lambda$testTraceDefer$14(HooksTraceTest.java:")); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
@Test public void log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db() throws Exception { CeTask ceTask = createCeTask(submitter); when(queue.peek(anyString())).thenReturn(Optional.of(ceTask)); taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor); IllegalStateException ex = makeTaskProcessorFail(ceTask); RuntimeException runtimeException = new RuntimeException("Simulate queue#remove failing"); doThrow(runtimeException).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex); underTest.call(); List<String> logs = logTester.logs(LoggerLevel.INFO); assertThat(logs).hasSize(2); assertThat(logs.get(0)).contains(" | submitter=" + submitter.getLogin()); assertThat(logs.get(1)).contains(String.format(" | submitter=%s | status=FAILED | time=", submitter.getLogin())); List<LogAndArguments> logAndArguments = logTester.getLogs(LoggerLevel.ERROR); assertThat(logAndArguments).hasSize(2); LogAndArguments executionErrorLog = logAndArguments.get(0); assertThat(executionErrorLog.getFormattedMsg()).isEqualTo("Failed to execute task " + ceTask.getUuid()); assertThat(executionErrorLog.getArgs().get()).containsOnly(ceTask.getUuid(), ex); LogAndArguments finalizingErrorLog = logAndArguments.get(1); assertThat(finalizingErrorLog.getFormattedMsg()).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db"); Object arg1 = finalizingErrorLog.getArgs().get()[0]; assertThat(arg1).isSameAs(runtimeException); assertThat(((Exception) arg1).getSuppressed()).containsOnly(ex); }
@Test public void testTrace3() throws Exception { Hooks.onOperatorDebug(); try { Flux.just(1) .map(d -> { throw new RuntimeException(); }) .share() .filter(d -> true) .doOnNext(d -> System.currentTimeMillis()) .map(d -> d) .blockLast(); } catch(Exception e){ e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains ("HooksTraceTest.java:")); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains("|_\tFlux.share ⇢ reactor.HooksTraceTest.testTrace3(HooksTraceTest.java:")); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
@Test public void testTraceComposed2() throws Exception { Hooks.onOperatorDebug(); try { Flux.just(1) .flatMap(d -> { throw new RuntimeException(); }) .filter(d -> true) .doOnNext(d -> System.currentTimeMillis()) .map(d -> d) .blockLast(); } catch(Exception e){ e.printStackTrace(); Assert.assertTrue(e.getSuppressed()[0].getMessage().contains ("HooksTraceTest.java:")); assertThat(e.getSuppressed()[0].getMessage()).contains("|_\tFlux.flatMap ⇢ reactor.HooksTraceTest.testTraceComposed2(HooksTraceTest.java:"); return; } finally { Hooks.resetOnOperatorDebug(); } throw new IllegalStateException(); }
for (final Throwable suppressed : e.getSuppressed()) for (final Throwable suppressed : e.getSuppressed())
logger.error(e.getMessage(), e); Throwable[] suppressed = e.getSuppressed(); if (suppressed != null) { for (Throwable t : suppressed) {
public static void main(String[] args) throws InterruptedException { String master = "https://localhost:8443/"; if (args.length == 1) { master = args[0]; } Config config = new ConfigBuilder().withMasterUrl(master) .withTrustCerts(true) .withUsername("admin") .withPassword("admin") .withNamespace("default") .build(); try (final KubernetesClient client = new AutoAdaptableKubernetesClient(config)) { log("Received pods", client.pods().list()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage(), e); Throwable[] suppressed = e.getSuppressed(); if (suppressed != null) { for (Throwable t : suppressed) { logger.error(t.getMessage(), t); } } } }
@Test public void withResourcesPreservesSuppressedExceptionThrownDuringClose() { RuntimeException rootException = new RuntimeException(); IOException nestedIOException = new IOException(); Try<Exception, Exception> failure = Try.withResources(() -> () -> { throw nestedIOException; }, resource -> { throw rootException; }); Exception thrown = failure.recover(id()); assertEquals(thrown, rootException); assertArrayEquals(new Throwable[]{nestedIOException}, thrown.getSuppressed()); }
private static <T extends Throwable> T copyContents(final Exception source, final T throwable) { throwable.setStackTrace(source.getStackTrace()); final Throwable[] suppressed = source.getSuppressed(); if (suppressed != null) for (final Throwable t : suppressed) { throwable.addSuppressed(t); } return throwable; } }
private static <T extends Throwable> T copyContents(final Exception source, final T throwable) { throwable.setStackTrace(source.getStackTrace()); final Throwable[] suppressed = source.getSuppressed(); if (suppressed != null) for (final Throwable t : suppressed) { throwable.addSuppressed(t); } return throwable; } }
private void writeObject(ObjectOutputStream out) throws IOException { ObjectOutputStream.PutField pf = out.putFields(); pf.put("message", super.getMessage()); Throwable cause = super.getCause(); if (cause == this) cause = null; pf.put("cause", cause); pf.put("stack", super.getStackTrace()); pf.put("suppressed", super.getSuppressed()); out.writeFields(); }
@Override public boolean matches(Object item) { if (!(item instanceof Exception)) { return false; } Exception that = (Exception) item; return Matchers.containsInAnyOrder(suppressions) .matches(ImmutableList.copyOf(that.getSuppressed())); } });
@Test public void testSuppressedIOException() throws IOException { try { IOUtils.forEach(Arrays.asList(1, 2, 3), (i) -> { throw new IOException("Number: " + i); }); } catch (Exception e) { assertEquals(2, e.getSuppressed().length); //two supressed exceptions and one thrown assertTrue(e instanceof IOException); assertEquals("Number: 1", e.getMessage()); } }
Throwable[] suppressed = e.getSuppressed(); if (suppressed != null) { for (Throwable t : suppressed) {