Java Code Examples for org.springframework.core.NestedExceptionUtils

The following examples show how to use org.springframework.core.NestedExceptionUtils. These examples are extracted from open source projects. 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
@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 2
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 3
Source Project: spring-analysis-note   Source File: NestedServletExceptionTests.java    License: 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 4
Source Project: spring-analysis-note   Source File: HttpWebHandlerAdapter.java    License: 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 5
@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 6
Source Project: cucumber-rest-steps   Source File: RestSteps.java    License: 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 7
Source Project: cucumber-rest-steps   Source File: RestSteps.java    License: 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 8
Source Project: java-spring-web   Source File: AbstractTracingClientTest.java    License: 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
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 10
@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 11
@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 12
@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 13
Source Project: spring-analysis-note   Source File: AbstractSockJsSession.java    License: 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 14
Source Project: spring-analysis-note   Source File: ResponseStatusException.java    License: 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 15
Source Project: spring-analysis-note   Source File: NestedServletException.java    License: 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 16
Source Project: java-technology-stack   Source File: AbstractSockJsSession.java    License: 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 Project: java-technology-stack   Source File: ResponseStatusException.java    License: 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 18
Source Project: java-technology-stack   Source File: HttpWebHandlerAdapter.java    License: 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 19
Source Project: java-technology-stack   Source File: NestedServletException.java    License: 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 20
Source Project: lams   Source File: NestedServletException.java    License: 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 21
/**
 * 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 22
/**
 * 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 23
private void handleErrNoSuchKeyError(NonTransientDataAccessException ex) {
	if (!"ERR no such key".equals(NestedExceptionUtils.getMostSpecificCause(ex).getMessage())) {
		throw ex;
	}
}