Java源码示例:org.springframework.core.NestedExceptionUtils
示例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"));
}
示例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");
}
}
示例3
@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());
}
示例4
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());
}
示例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());
}
示例6
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);
}
}
示例7
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);
}
}
示例8
@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"));
}
示例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;
}
示例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());
}
示例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);
}
}
示例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);
}
}
示例13
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));
}
示例14
@Override
public String getMessage() {
String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
}
示例15
/**
* 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());
}
示例16
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));
}
示例17
@Override
public String getMessage() {
String msg = this.status + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
}
示例18
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));
}
示例19
/**
* 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());
}
示例20
/**
* 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());
}
示例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());
}
示例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());
}
示例23
private void handleErrNoSuchKeyError(NonTransientDataAccessException ex) {
if (!"ERR no such key".equals(NestedExceptionUtils.getMostSpecificCause(ex).getMessage())) {
throw ex;
}
}