Replace deep exception message nesting with custom inclusion of cause messages

Includes deprecation of NestedServletException, whereas NestedCheckedException and NestedRuntimeException remain as base classes with several convenience methods.

Closes gh-25162
This commit is contained in:
Juergen Hoeller 2022-06-14 14:00:28 +02:00
parent 933965b7b4
commit 4e1b9f1492
31 changed files with 78 additions and 245 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -71,7 +71,8 @@ public class TypeMismatchException extends PropertyAccessException {
(requiredType != null ? (requiredType != null ?
" to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") + " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(propertyChangeEvent.getPropertyName() != null ? (propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""), " for property '" + propertyChangeEvent.getPropertyName() + "'" : "") +
(cause != null ? "; " + cause.getMessage() : ""),
cause); cause);
this.propertyName = propertyChangeEvent.getPropertyName(); this.propertyName = propertyChangeEvent.getPropertyName();
this.value = propertyChangeEvent.getNewValue(); this.value = propertyChangeEvent.getNewValue();
@ -97,7 +98,8 @@ public class TypeMismatchException extends PropertyAccessException {
*/ */
public TypeMismatchException(@Nullable Object value, @Nullable Class<?> requiredType, @Nullable Throwable cause) { public TypeMismatchException(@Nullable Object value, @Nullable Class<?> requiredType, @Nullable Throwable cause) {
super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" + super("Failed to convert value of type '" + ClassUtils.getDescriptiveType(value) + "'" +
(requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : ""), (requiredType != null ? " to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(cause != null ? "; " + cause.getMessage() : ""),
cause); cause);
this.value = value; this.value = value;
this.requiredType = requiredType; this.requiredType = requiredType;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,7 +62,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
public UnsatisfiedDependencyException( public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, BeansException ex) { @Nullable String resourceDescription, @Nullable String beanName, String propertyName, BeansException ex) {
this(resourceDescription, beanName, propertyName, ""); this(resourceDescription, beanName, propertyName, ex.getMessage());
initCause(ex); initCause(ex);
} }
@ -94,7 +94,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
public UnsatisfiedDependencyException( public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, BeansException ex) { @Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, BeansException ex) {
this(resourceDescription, beanName, injectionPoint, ""); this(resourceDescription, beanName, injectionPoint, ex.getMessage());
initCause(ex); initCause(ex);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -86,7 +86,7 @@ public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
processProperties(beanFactory, mergedProps); processProperties(beanFactory, mergedProps);
} }
catch (IOException ex) { catch (IOException ex) {
throw new BeanInitializationException("Could not load properties", ex); throw new BeanInitializationException("Could not load properties: " + ex.getMessage(), ex);
} }
} }

View File

@ -608,8 +608,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
throw (BeanCreationException) ex; throw (BeanCreationException) ex;
} }
else { else {
throw new BeanCreationException( throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
} }
} }
@ -1302,8 +1301,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return bw; return bw;
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanCreationException( throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
} }
} }
@ -1699,8 +1697,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
bw.setPropertyValues(new MutablePropertyValues(deepCopy)); bw.setPropertyValues(new MutablePropertyValues(deepCopy));
} }
catch (BeansException ex) { catch (BeansException ex) {
throw new BeanCreationException( throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
mbd.getResourceDescription(), beanName, "Error setting property values", ex);
} }
} }
@ -1752,8 +1749,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanCreationException( throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null), (mbd != null ? mbd.getResourceDescription() : null), beanName, ex.getMessage(), ex);
beanName, "Invocation of init method failed", ex);
} }
if (mbd == null || !mbd.isSynthetic()) { if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

View File

@ -303,8 +303,7 @@ class ConstructorResolver {
return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse); return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
"Bean instantiation via constructor failed", ex);
} }
} }
@ -631,8 +630,7 @@ class ConstructorResolver {
mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args); mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args);
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
"Bean instantiation via factory method failed", ex);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -152,7 +152,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
"Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex); "Cannot access factory method '" + factoryMethod.getName() + "'; is it public?", ex);
} }
catch (InvocationTargetException ex) { catch (InvocationTargetException ex) {
String msg = "Factory method '" + factoryMethod.getName() + "' threw exception"; String msg = ex.getTargetException().getMessage();
if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory && if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory &&
((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) { ((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) {
msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " + msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +

View File

@ -606,7 +606,7 @@ class ConfigurationClassParser {
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanDefinitionStoreException( throw new BeanDefinitionStoreException(
"Failed to process import candidates for configuration class [" + "Failed to process import candidates for configuration class [" +
configClass.getMetadata().getClassName() + "]", ex); configClass.getMetadata().getClassName() + "]: " + ex.getMessage(), ex);
} }
finally { finally {
this.importStack.pop(); this.importStack.pop();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -155,7 +155,7 @@ public class EventListenerMethodProcessor
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new BeanInitializationException("Failed to process @EventListener " + throw new BeanInitializationException("Failed to process @EventListener " +
"annotation on bean with name '" + beanName + "'", ex); "annotation on bean with name '" + beanName + "': " + ex.getMessage(), ex);
} }
} }
} }

View File

@ -944,8 +944,8 @@ class XmlBeanFactoryTests {
xbf.getBean("rod2Accessor"); xbf.getBean("rod2Accessor");
} }
catch (BeanCreationException ex) { catch (BeanCreationException ex) {
assertThat(ex.toString().contains("touchy")).isTrue();
ex.printStackTrace(); ex.printStackTrace();
assertThat(ex.toString().contains("touchy")).isTrue();
assertThat((Object) ex.getRelatedCauses()).isNull(); assertThat((Object) ex.getRelatedCauses()).isNull();
} }
} }
@ -1370,7 +1370,7 @@ class XmlBeanFactoryTests {
reader.loadBeanDefinitions(INVALID_NO_SUCH_METHOD_CONTEXT); reader.loadBeanDefinitions(INVALID_NO_SUCH_METHOD_CONTEXT);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() -> assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
xbf.getBean("constructorOverrides")) xbf.getBean("constructorOverrides"))
.withMessageContaining("bogusMethod"); .satisfies(ex -> ex.getCause().getMessage().contains("bogusMethod"));
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -56,7 +56,7 @@ public class Spr12278Tests {
public void componentTwoSpecificConstructorsNoHint() { public void componentTwoSpecificConstructorsNoHint() {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
new AnnotationConfigApplicationContext(BaseConfiguration.class, TwoSpecificConstructorsComponent.class)) new AnnotationConfigApplicationContext(BaseConfiguration.class, TwoSpecificConstructorsComponent.class))
.withMessageContaining(NoSuchMethodException.class.getName()); .withMessageContaining("No default constructor found");
} }

View File

@ -33,7 +33,6 @@ import org.springframework.lang.Nullable;
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @see #getMessage * @see #getMessage
* @see #printStackTrace
* @see NestedRuntimeException * @see NestedRuntimeException
*/ */
public abstract class NestedCheckedException extends Exception { public abstract class NestedCheckedException extends Exception {
@ -41,12 +40,6 @@ public abstract class NestedCheckedException extends Exception {
/** Use serialVersionUID from Spring 1.2 for interoperability. */ /** Use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = 7100714597678207546L; private static final long serialVersionUID = 7100714597678207546L;
static {
// Eagerly load the NestedExceptionUtils class to avoid classloader deadlock
// issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607.
NestedExceptionUtils.class.getName();
}
/** /**
* Construct a {@code NestedCheckedException} with the specified detail message. * Construct a {@code NestedCheckedException} with the specified detail message.
@ -67,17 +60,6 @@ public abstract class NestedCheckedException extends Exception {
} }
/**
* 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());
}
/** /**
* Retrieve the innermost cause of this exception, if any. * Retrieve the innermost cause of this exception, if any.
* @return the innermost exception, or {@code null} if none * @return the innermost exception, or {@code null} if none

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,8 +29,6 @@ import org.springframework.lang.Nullable;
* @since 2.0 * @since 2.0
* @see NestedRuntimeException * @see NestedRuntimeException
* @see NestedCheckedException * @see NestedCheckedException
* @see NestedIOException
* @see org.springframework.web.util.NestedServletException
*/ */
public abstract class NestedExceptionUtils { public abstract class NestedExceptionUtils {
@ -39,7 +37,10 @@ public abstract class NestedExceptionUtils {
* @param message the base message * @param message the base message
* @param cause the root cause * @param cause the root cause
* @return the full exception message * @return the full exception message
* @deprecated as of 6.0, in favor of custom exception messages
* with selective inclusion of cause messages
*/ */
@Deprecated
@Nullable @Nullable
public static String buildMessage(@Nullable String message, @Nullable Throwable cause) { public static String buildMessage(@Nullable String message, @Nullable Throwable cause) {
if (cause == null) { if (cause == null) {

View File

@ -33,7 +33,6 @@ import org.springframework.lang.Nullable;
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
* @see #getMessage * @see #getMessage
* @see #printStackTrace
* @see NestedCheckedException * @see NestedCheckedException
*/ */
public abstract class NestedRuntimeException extends RuntimeException { public abstract class NestedRuntimeException extends RuntimeException {
@ -41,12 +40,6 @@ public abstract class NestedRuntimeException extends RuntimeException {
/** Use serialVersionUID from Spring 1.2 for interoperability. */ /** Use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = 5439915454935047936L; private static final long serialVersionUID = 5439915454935047936L;
static {
// Eagerly load the NestedExceptionUtils class to avoid classloader deadlock
// issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607.
NestedExceptionUtils.class.getName();
}
/** /**
* Construct a {@code NestedRuntimeException} with the specified detail message. * Construct a {@code NestedRuntimeException} with the specified detail message.
@ -67,17 +60,6 @@ public abstract class NestedRuntimeException extends RuntimeException {
} }
/**
* 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());
}
/** /**
* Retrieve the innermost cause of this exception, if any. * Retrieve the innermost cause of this exception, if any.
* @return the innermost exception, or {@code null} if none * @return the innermost exception, or {@code null} if none

View File

@ -1,109 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
@SuppressWarnings("serial")
class NestedExceptionTests {
@Test
void nestedRuntimeExceptionWithNoRootCause() {
String mesg = "mesg of mine";
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedRuntimeException nex = new NestedRuntimeException(mesg) {};
assertThat(nex.getCause()).isNull();
assertThat(mesg).isEqualTo(nex.getMessage());
// Check printStackTrace
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
nex.printStackTrace(pw);
pw.flush();
String stackTrace = new String(baos.toByteArray());
assertThat(stackTrace.contains(mesg)).isTrue();
}
@Test
void nestedRuntimeExceptionWithRootCause() {
String myMessage = "mesg for this exception";
String rootCauseMsg = "this is the obscure message of the root cause";
Exception rootCause = new Exception(rootCauseMsg);
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {};
assertThat(rootCause).isEqualTo(nex.getCause());
assertThat(nex.getMessage().contains(myMessage)).isTrue();
assertThat(nex.getMessage().endsWith(rootCauseMsg)).isTrue();
// check PrintStackTrace
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
nex.printStackTrace(pw);
pw.flush();
String stackTrace = new String(baos.toByteArray());
assertThat(stackTrace.contains(rootCause.getClass().getName())).isTrue();
assertThat(stackTrace.contains(rootCauseMsg)).isTrue();
}
@Test
void nestedCheckedExceptionWithNoRootCause() {
String mesg = "mesg of mine";
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedCheckedException nex = new NestedCheckedException(mesg) {};
assertThat(nex.getCause()).isNull();
assertThat(mesg).isEqualTo(nex.getMessage());
// Check printStackTrace
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
nex.printStackTrace(pw);
pw.flush();
String stackTrace = new String(baos.toByteArray());
assertThat(stackTrace.contains(mesg)).isTrue();
}
@Test
void nestedCheckedExceptionWithRootCause() {
String myMessage = "mesg for this exception";
String rootCauseMsg = "this is the obscure message of the root cause";
Exception rootCause = new Exception(rootCauseMsg);
// Making a class abstract doesn't _really_ prevent instantiation :-)
NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {};
assertThat(rootCause).isEqualTo(nex.getCause());
assertThat(nex.getMessage().contains(myMessage)).isTrue();
assertThat(nex.getMessage().endsWith(rootCauseMsg)).isTrue();
// check PrintStackTrace
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
nex.printStackTrace(pw);
pw.flush();
String stackTrace = new String(baos.toByteArray());
assertThat(stackTrace.contains(rootCause.getClass().getName())).isTrue();
assertThat(stackTrace.contains(rootCauseMsg)).isTrue();
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -473,7 +473,8 @@ public class JmsMessagingTemplate extends AbstractMessagingTemplate<Destination>
return this.messageConverter.toMessage(this.message, session); return this.messageConverter.toMessage(this.message, session);
} }
catch (Exception ex) { catch (Exception ex) {
throw new MessageConversionException("Could not convert '" + this.message + "'", ex); throw new MessageConversionException(
"Could not convert '" + this.message + "': " + ex.getMessage(), ex);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2019-2020 the original author or authors. * Copyright 2019-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -105,8 +105,7 @@ public class ConnectionFactoryUtilsUnitTests {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException("MESSAGE")); "SOME-SQL", new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isInstanceOf( assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage( TransientDataAccessResourceException.class).hasMessage("TASK; SQL [SOME-SQL]; MESSAGE");
"TASK; SQL [SOME-SQL]; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE");
} }
@Test @Test
@ -114,21 +113,20 @@ public class ConnectionFactoryUtilsUnitTests {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null, Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null,
new R2dbcTransientResourceException("MESSAGE")); new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isInstanceOf( assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage( TransientDataAccessResourceException.class).hasMessage("TASK; MESSAGE");
"TASK; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE");
} }
@Test @Test
public void messageGenerationNullMessage() { public void messageGenerationNullMessage() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException()); "SOME-SQL", new R2dbcTransientResourceException());
assertThat(exception).isInstanceOf( assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage( TransientDataAccessResourceException.class).hasMessage("TASK; SQL [SOME-SQL]; null");
"TASK; SQL [SOME-SQL]; null; nested exception is io.r2dbc.spi.R2dbcTransientResourceException");
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
private static class MyTransientExceptions extends R2dbcException { private static class MyTransientExceptions extends R2dbcException {
} }
} }

View File

@ -75,7 +75,7 @@ public abstract class AbstractXmlHttpMessageConverter<T> extends AbstractHttpMes
throw ex; throw ex;
} }
catch (Exception ex) { catch (Exception ex) {
throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex,
ex, inputMessage); ex, inputMessage);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -173,7 +173,7 @@ public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
} }
catch (UnmarshalException ex) { catch (UnmarshalException ex) {
throw new HttpMessageNotReadableException( throw new HttpMessageNotReadableException(
"Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex, inputMessage); "Could not unmarshal to [" + elementClass + "]: " + ex, ex, inputMessage);
} }
catch (JAXBException ex) { catch (JAXBException ex) {
throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex); throw new HttpMessageConversionException("Invalid JAXB setup: " + ex.getMessage(), ex);

View File

@ -18,14 +18,12 @@ package org.springframework.web;
import java.net.URI; import java.net.URI;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.core.NestedRuntimeException; import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
* {@link RuntimeException} that implements {@link ErrorResponse} to expose * {@link RuntimeException} that implements {@link ErrorResponse} to expose
* an HTTP status, response headers, and a body formatted as an RFC 7808 * an HTTP status, response headers, and a body formatted as an RFC 7808
@ -137,8 +135,7 @@ public class ErrorResponseException extends NestedRuntimeException implements Er
@Override @Override
public String getMessage() { public String getMessage() {
String message = this.status + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body; return this.status + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
return NestedExceptionUtils.buildMessage(message, getCause());
} }
} }

View File

@ -16,11 +16,12 @@
package org.springframework.web.bind; package org.springframework.web.bind;
import jakarta.servlet.ServletException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail; import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse; import org.springframework.web.ErrorResponse;
import org.springframework.web.util.NestedServletException;
/** /**
* Fatal binding exception, thrown when we want to * Fatal binding exception, thrown when we want to
@ -34,7 +35,7 @@ import org.springframework.web.util.NestedServletException;
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ServletRequestBindingException extends NestedServletException implements ErrorResponse { public class ServletRequestBindingException extends ServletException implements ErrorResponse {
private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode()); private final ProblemDetail body = ProblemDetail.forStatus(getStatusCode());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -49,7 +49,6 @@ import org.springframework.util.StringUtils;
import org.springframework.web.context.ServletContextAware; import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResourceLoader; import org.springframework.web.context.support.ServletContextResourceLoader;
import org.springframework.web.context.support.StandardServletEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
import org.springframework.web.util.NestedServletException;
/** /**
* Simple base implementation of {@link jakarta.servlet.Filter} which treats * Simple base implementation of {@link jakarta.servlet.Filter} which treats
@ -228,9 +227,9 @@ public abstract class GenericFilterBean implements Filter, BeanNameAware, Enviro
} }
catch (BeansException ex) { catch (BeansException ex) {
String msg = "Failed to set bean properties on filter '" + String msg = "Failed to set bean properties on filter '" +
filterConfig.getFilterName() + "': " + ex.getMessage(); filterConfig.getFilterName() + "': " + ex.getMessage();
logger.error(msg, ex); logger.error(msg, ex);
throw new NestedServletException(msg, ex); throw new ServletException(msg, ex);
} }
} }

View File

@ -16,7 +16,6 @@
package org.springframework.web.server; package org.springframework.web.server;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode; import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -112,8 +111,7 @@ public class ResponseStatusException extends ErrorResponseException {
@Override @Override
public String getMessage() { public String getMessage() {
String msg = getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : ""); return getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,10 +37,11 @@ import org.springframework.lang.Nullable;
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.2.5 * @since 1.2.5
* @see #getMessage * @see #getMessage
* @see #printStackTrace
* @see org.springframework.core.NestedCheckedException * @see org.springframework.core.NestedCheckedException
* @see org.springframework.core.NestedRuntimeException * @see org.springframework.core.NestedRuntimeException
* @deprecated as of 6.0, in favor of standard {@link ServletException} nesting
*/ */
@Deprecated
public class NestedServletException extends ServletException { public class NestedServletException extends ServletException {
/** Use serialVersionUID from Spring 1.2 for interoperability. */ /** Use serialVersionUID from Spring 1.2 for interoperability. */
@ -68,18 +69,7 @@ public class NestedServletException extends ServletException {
* @param cause the nested exception * @param cause the nested exception
*/ */
public NestedServletException(@Nullable String msg, @Nullable Throwable cause) { public NestedServletException(@Nullable String msg, @Nullable Throwable cause) {
super(msg, cause); super(NestedExceptionUtils.buildMessage(msg, cause), cause);
}
/**
* 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());
} }
} }

View File

@ -598,8 +598,7 @@ class RestTemplateTests {
assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() -> assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() ->
template.getForObject(url, String.class)) template.getForObject(url, String.class))
.withMessage("I/O error on GET request for \"https://example.com/resource\": " + .withMessage("I/O error on GET request for \"https://example.com/resource\": Socket failure");
"Socket failure; nested exception is java.io.IOException: Socket failure");
} }
@Test // SPR-15900 @Test // SPR-15900
@ -615,8 +614,7 @@ class RestTemplateTests {
assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() -> assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() ->
template.getForObject(uri, String.class)) template.getForObject(uri, String.class))
.withMessage("I/O error on GET request for \"https://example.com/resource\": " + .withMessage("I/O error on GET request for \"https://example.com/resource\": Socket failure");
"Socket failure; nested exception is java.io.IOException: Socket failure");
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -47,7 +47,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
cll.contextInitialized(new ServletContextEvent(sc))) cll.contextInitialized(new ServletContextEvent(sc)))
.withMessageEndingWith("Could not open ServletContext resource [/programmatic.xml]"); .withMessageEndingWith("ServletContext resource [/programmatic.xml]");
} }
/** /**
@ -68,7 +68,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
cll.contextInitialized(new ServletContextEvent(sc))) cll.contextInitialized(new ServletContextEvent(sc)))
.withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]"); .withMessageEndingWith("ServletContext resource [/from-init-param.xml]");
} }
/** /**
@ -86,7 +86,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
cll.contextInitialized(new ServletContextEvent(sc))) cll.contextInitialized(new ServletContextEvent(sc)))
.withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]"); .withMessageEndingWith("ServletContext resource [/from-init-param.xml]");
} }
/** /**
@ -108,7 +108,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
cll.contextInitialized(new ServletContextEvent(sc))) cll.contextInitialized(new ServletContextEvent(sc)))
.withMessageEndingWith("Could not open ServletContext resource [/from-init-param.xml]"); .withMessageEndingWith("ServletContext resource [/from-init-param.xml]");
} }
/** /**
@ -127,7 +127,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
cll.contextInitialized(new ServletContextEvent(sc))) cll.contextInitialized(new ServletContextEvent(sc)))
.withMessageEndingWith("Could not open ServletContext resource [/WEB-INF/applicationContext.xml]"); .withMessageEndingWith("ServletContext resource [/WEB-INF/applicationContext.xml]");
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,7 +62,6 @@ import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.util.NestedServletException;
import org.springframework.web.util.ServletRequestPathUtils; import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
@ -1077,7 +1076,7 @@ public class DispatcherServlet extends FrameworkServlet {
catch (Throwable err) { catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well, // As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios. // making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err); dispatchException = new ServletException("Handler dispatch failed: " + err, err);
} }
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException); processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
} }
@ -1086,7 +1085,7 @@ public class DispatcherServlet extends FrameworkServlet {
} }
catch (Throwable err) { catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler, triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err)); new ServletException("Handler processing failed: " + err, err));
} }
finally { finally {
if (asyncManager.isConcurrentHandlingStarted()) { if (asyncManager.isConcurrentHandlingStarted()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -68,7 +68,6 @@ import org.springframework.web.context.support.ServletRequestHandledEvent;
import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.cors.CorsUtils; import org.springframework.web.cors.CorsUtils;
import org.springframework.web.util.NestedServletException;
import org.springframework.web.util.WebUtils; import org.springframework.web.util.WebUtils;
/** /**
@ -1009,7 +1008,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
} }
catch (Throwable ex) { catch (Throwable ex) {
failureCause = ex; failureCause = ex;
throw new NestedServletException("Request processing failed", ex); throw new ServletException("Request processing failed: " + ex, ex);
} }
finally { finally {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -44,7 +45,6 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandlerCom
import org.springframework.web.method.support.InvocableHandlerMethod; import org.springframework.web.method.support.InvocableHandlerMethod;
import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.View; import org.springframework.web.servlet.View;
import org.springframework.web.util.NestedServletException;
/** /**
* Extends {@link InvocableHandlerMethod} with the ability to handle return * Extends {@link InvocableHandlerMethod} with the ability to handle return
@ -220,7 +220,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
throw (Exception) result; throw (Exception) result;
} }
else if (result instanceof Throwable) { else if (result instanceof Throwable) {
throw new NestedServletException("Async processing failed", (Throwable) result); throw new ServletException("Async processing failed: " + result, (Throwable) result);
} }
return result; return result;
}, CALLABLE_METHOD); }, CALLABLE_METHOD);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.Map;
import groovy.text.Template; import groovy.text.Template;
import groovy.text.markup.MarkupTemplateEngine; import groovy.text.markup.MarkupTemplateEngine;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -34,7 +35,6 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.servlet.view.AbstractTemplateView; import org.springframework.web.servlet.view.AbstractTemplateView;
import org.springframework.web.util.NestedServletException;
/** /**
* An {@link AbstractTemplateView} subclass based on Groovy XML/XHTML markup templates. * An {@link AbstractTemplateView} subclass based on Groovy XML/XHTML markup templates.
@ -133,7 +133,7 @@ public class GroovyMarkupView extends AbstractTemplateView {
} }
catch (ClassNotFoundException ex) { catch (ClassNotFoundException ex) {
Throwable cause = (ex.getCause() != null ? ex.getCause() : ex); Throwable cause = (ex.getCause() != null ? ex.getCause() : ex);
throw new NestedServletException( throw new ServletException(
"Could not find class while rendering Groovy Markup view with name '" + "Could not find class while rendering Groovy Markup view with name '" +
getUrl() + "': " + ex.getMessage() + "'", cause); getUrl() + "': " + ex.getMessage() + "'", cause);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.net.SocketTimeoutException;
import java.util.Collections; import java.util.Collections;
import java.util.Locale; import java.util.Locale;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -61,7 +62,6 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest; import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse; import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.util.NestedServletException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -315,7 +315,7 @@ public class ExceptionHandlerExceptionResolverTests {
AssertionError err = new AssertionError("argh"); AssertionError err = new AssertionError("argh");
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle"); HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod, ModelAndView mav = this.resolver.resolveException(this.request, this.response, handlerMethod,
new NestedServletException("Handler dispatch failed", err)); new ServletException("Handler dispatch failed", err));
assertThat(mav).as("Exception was not handled").isNotNull(); assertThat(mav).as("Exception was not handled").isNotNull();
assertThat(mav.isEmpty()).isTrue(); assertThat(mav.isEmpty()).isTrue();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -181,7 +181,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
failure = ex; failure = ex;
} }
catch (Exception ex) { catch (Exception ex) {
failure = new HandshakeFailureException("Uncaught failure for request " + request.getURI(), ex); failure = new HandshakeFailureException(
"Uncaught failure for request " + request.getURI() + " - " + ex.getMessage(), ex);
} }
finally { finally {
if (failure != null) { if (failure != null) {