org.springframework.core.NestedExceptionUtils Java Examples

The following examples show how to use org.springframework.core.NestedExceptionUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PostReleaseActions.java    From spring-cloud-release-tools with Apache License 2.0 6 votes vote down vote up
private void assertExceptions(List<ProjectUrlAndException> projectUrlAndExceptions) {
	String exceptionMessages = projectUrlAndExceptions.stream()
			.filter(ProjectUrlAndException::hasException)
			.map(e -> "Project [" + e.key + "] for url [" + e.url + "] "
					+ "has exception [\n\n"
					+ Arrays.stream(NestedExceptionUtils.getMostSpecificCause(e.ex)
							.getStackTrace()).map(StackTraceElement::toString)
							.collect(Collectors.joining("\n"))
					+ "]")
			.collect(Collectors.joining("\n"));
	if (StringUtils.hasText(exceptionMessages)) {
		throw new IllegalStateException(
				"Exceptions were found while running post release tasks\n"
						+ exceptionMessages);
	}
	else {
		log.info("No exceptions were found while running post release tasks");
	}
}
 
Example #2
Source File: CustomSpanDecoratorAutoConfigurationTest.java    From java-spring-web with Apache License 2.0 6 votes vote down vote up
@Ignore("Fix me, I'm flaky!")
@Test
public void testWebClientCustomTracing() {
    try {
        webClient.get()
                .uri(URI.create("http://nonexisting.example.com"))
                .retrieve()
                .bodyToMono(String.class)
                .block();
    } catch (final RuntimeException e) {
        Assert.assertTrue(NestedExceptionUtils.getRootCause(e) instanceof UnknownHostException);
    }

    Assert.assertEquals(1, mockTracer.finishedSpans().size());
    Assert.assertEquals("foo", mockTracer.finishedSpans().get(0).tags().get("custom-test"));
}
 
Example #3
Source File: RestSteps.java    From cucumber-rest-steps with MIT License 5 votes vote down vote up
private void withAssertion(Consumer<Void> consumer) {
  try {
    consumer.accept(null);
  } catch (AssertionError error) {
    initExpectBody();
    final EntityExchangeResult<byte[]> entityExchangeResult = expectBody.returnResult();
    throw new AssertionError(NestedExceptionUtils.getMostSpecificCause(error).getMessage() + "\n" + entityExchangeResult);
  }
}
 
Example #4
Source File: NativeEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public Environment findOne(String config, String profile, String label,
		boolean includeOrigin) {
	SpringApplicationBuilder builder = new SpringApplicationBuilder(
			PropertyPlaceholderAutoConfiguration.class);
	ConfigurableEnvironment environment = getEnvironment(profile);
	builder.environment(environment);
	builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF);
	if (!logger.isDebugEnabled()) {
		// Make the mini-application startup less verbose
		builder.logStartupInfo(false);
	}
	String[] args = getArgs(config, profile, label);
	// Explicitly set the listeners (to exclude logging listener which would change
	// log levels in the caller)
	builder.application()
			.setListeners(Arrays.asList(new ConfigFileApplicationListener()));

	try (ConfigurableApplicationContext context = builder.run(args)) {
		environment.getPropertySources().remove("profiles");
		return clean(new PassthruEnvironmentRepository(environment).findOne(config,
				profile, label, includeOrigin));
	}
	catch (Exception e) {
		String msg = String.format(
				"Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",
				config, profile, label, includeOrigin);
		String completeMessage = NestedExceptionUtils.buildMessage(msg,
				NestedExceptionUtils.getMostSpecificCause(e));
		throw new FailedToConstructEnvironmentException(completeMessage, e);
	}
}
 
Example #5
Source File: CompatibilityVerifierFailureAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void contextFailsToLoad() {
	try {
		SpringApplication.run(TestConfiguration.class,
				"--spring.cloud.compatibility-verifier.compatible-boot-versions=1.2.x,1.3.x");
		BDDAssertions.fail("should throw exception");
	}
	catch (BeanCreationException ex) {
		Throwable cause = NestedExceptionUtils.getRootCause(ex);
		then(((CompatibilityNotMetException) cause).results).hasSize(1);
	}
}
 
Example #6
Source File: NestedServletExceptionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedServletExceptionStringThrowable() {
	Throwable cause = new RuntimeException();
	NestedServletException exception = new NestedServletException("foo", cause);
	assertEquals(NestedExceptionUtils.buildMessage("foo", cause), exception.getMessage());
	assertEquals(cause, exception.getCause());
}
 
Example #7
Source File: SpringBatchBuildReportHandler.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
Table(String creationTime, String executionTime, String projectName,
		String taskCaption, String taskDescription, String taskState,
		List<Throwable> exceptions) {
	this.creationTime = creationTime;
	this.executionTime = executionTime;
	this.projectName = projectName;
	this.taskCaption = taskCaption;
	this.taskDescription = taskDescription;
	this.taskState = taskState;
	this.thrownException = exceptions == null ? "" : exceptions.stream()
			// TODO: Last but most specific
			.map(t -> NestedExceptionUtils.getMostSpecificCause(t).toString())
			.collect(Collectors.joining("\n"));
	this.exceptions = exceptions;
}
 
Example #8
Source File: AbstractTracingClientTest.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorUnknownHostException() {
    String url = "http://nonexisting.example.com";
    try {
        client.getForEntity(url, String.class);
    } catch (RuntimeException ex) {
        Assert.assertTrue(NestedExceptionUtils.getRootCause(ex) instanceof UnknownHostException);
        //ok UnknownHostException
    }

    List<MockSpan> mockSpans = mockTracer.finishedSpans();
    Assert.assertEquals(1, mockSpans.size());

    MockSpan mockSpan = mockSpans.get(0);
    Assert.assertEquals("GET", mockSpan.operationName());
    Assert.assertEquals(5, mockSpan.tags().size());
    Assert.assertEquals(componentName, mockSpan.tags().get(Tags.COMPONENT.getKey()));
    Assert.assertEquals(Tags.SPAN_KIND_CLIENT, mockSpan.tags().get(Tags.SPAN_KIND.getKey()));
    Assert.assertEquals("GET", mockSpan.tags().get(Tags.HTTP_METHOD.getKey()));
    Assert.assertEquals(url, mockSpan.tags().get(Tags.HTTP_URL.getKey()));
    Assert.assertEquals(Boolean.TRUE, mockSpan.tags().get(Tags.ERROR.getKey()));

    Assert.assertEquals(1, mockSpan.logEntries().size());
    Assert.assertEquals(2, mockSpan.logEntries().get(0).fields().size());
    Assert.assertEquals(Tags.ERROR.getKey(), mockSpan.logEntries().get(0).fields().get("event"));
    Assert.assertNotNull(mockSpan.logEntries().get(0).fields().get("error.object"));
}
 
Example #9
Source File: NestedServletExceptionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testNestedServletExceptionStringThrowable() {
	Throwable cause = new RuntimeException();
	NestedServletException exception = new NestedServletException("foo", cause);
	assertEquals(NestedExceptionUtils.buildMessage("foo", cause), exception.getMessage());
	assertEquals(cause, exception.getCause());
}
 
Example #10
Source File: RestSteps.java    From cucumber-rest-steps with MIT License 5 votes vote down vote up
private <T> T withAssertion(Supplier<T> supplier) {
  try {
    return supplier.get();
  } catch (AssertionError error) {
    initExpectBody();
    final EntityExchangeResult<byte[]> entityExchangeResult = expectBody.returnResult();
    throw new AssertionError(NestedExceptionUtils.getMostSpecificCause(error).getMessage() + "\n" + entityExchangeResult);
  }
}
 
Example #11
Source File: NestedServletExceptionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testNestedServletExceptionStringThrowable() {
	Throwable cause = new RuntimeException();
	NestedServletException exception = new NestedServletException("foo", cause);
	assertEquals(NestedExceptionUtils.buildMessage("foo", cause), exception.getMessage());
	assertEquals(cause, exception.getCause());
}
 
Example #12
Source File: HttpWebHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private boolean isDisconnectedClientError(Throwable ex) {
	String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
	if (message != null) {
		String text = message.toLowerCase();
		if (text.contains("broken pipe") || text.contains("connection reset by peer")) {
			return true;
		}
	}
	return DISCONNECTED_CLIENT_EXCEPTIONS.contains(ex.getClass().getSimpleName());
}
 
Example #13
Source File: NestedServletException.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the detail message, including the message from the nested exception
 * if there is one.
 */
@Override
@Nullable
public String getMessage() {
	return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
}
 
Example #14
Source File: HttpWebHandlerAdapter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean isDisconnectedClientError(Throwable ex)  {
	String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
	message = (message != null ? message.toLowerCase() : "");
	String className = ex.getClass().getSimpleName();
	return (message.contains("broken pipe") || DISCONNECTED_CLIENT_EXCEPTIONS.contains(className));
}
 
Example #15
Source File: ResponseStatusException.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public String getMessage() {
	String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");
	return NestedExceptionUtils.buildMessage(msg, getCause());
}
 
Example #16
Source File: AbstractSockJsSession.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean indicatesDisconnectedClient(Throwable ex)  {
	String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
	message = (message != null ? message.toLowerCase() : "");
	String className = ex.getClass().getSimpleName();
	return (message.contains("broken pipe") || DISCONNECTED_CLIENT_EXCEPTIONS.contains(className));
}
 
Example #17
Source File: NestedServletException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the detail message, including the message from the nested exception
 * if there is one.
 */
@Override
@Nullable
public String getMessage() {
	return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
}
 
Example #18
Source File: NestedServletException.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the detail message, including the message from the nested exception
 * if there is one.
 */
@Override
public String getMessage() {
	return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
}
 
Example #19
Source File: NestedServletException.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the detail message, including the message from the nested exception
 * if there is one.
 */
@Override
public String getMessage() {
	return NestedExceptionUtils.buildMessage(super.getMessage(), getCause());
}
 
Example #20
Source File: SpringWebfluxApiExceptionHandler.java    From backstopper with Apache License 2.0 4 votes vote down vote up
/**
 * Copied from Spring Boot 2's {@code AbstractErrorWebExceptionHandler} class.
 */
public static boolean isDisconnectedClientError(Throwable ex) {
    return DISCONNECTED_CLIENT_EXCEPTIONS.contains(ex.getClass().getSimpleName())
           || isDisconnectedClientErrorMessage(NestedExceptionUtils.getMostSpecificCause(ex).getMessage());
}
 
Example #21
Source File: ResponseStatusException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public String getMessage() {
	String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");
	return NestedExceptionUtils.buildMessage(msg, getCause());
}
 
Example #22
Source File: RedisIndexedSessionRepository.java    From spring-session with Apache License 2.0 4 votes vote down vote up
private void handleErrNoSuchKeyError(NonTransientDataAccessException ex) {
	if (!"ERR no such key".equals(NestedExceptionUtils.getMostSpecificCause(ex).getMessage())) {
		throw ex;
	}
}
 
Example #23
Source File: AbstractSockJsSession.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private boolean indicatesDisconnectedClient(Throwable ex)  {
	String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
	message = (message != null ? message.toLowerCase() : "");
	String className = ex.getClass().getSimpleName();
	return (message.contains("broken pipe") || DISCONNECTED_CLIENT_EXCEPTIONS.contains(className));
}