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");
* you may not use this file except in compliance with the License.
@ -71,7 +71,8 @@ public class TypeMismatchException extends PropertyAccessException {
(requiredType != null ?
" to required type '" + ClassUtils.getQualifiedName(requiredType) + "'" : "") +
(propertyChangeEvent.getPropertyName() != null ?
" for property '" + propertyChangeEvent.getPropertyName() + "'" : ""),
" for property '" + propertyChangeEvent.getPropertyName() + "'" : "") +
(cause != null ? "; " + cause.getMessage() : ""),
cause);
this.propertyName = propertyChangeEvent.getPropertyName();
this.value = propertyChangeEvent.getNewValue();
@ -97,7 +98,8 @@ public class TypeMismatchException extends PropertyAccessException {
*/
public TypeMismatchException(@Nullable Object value, @Nullable Class<?> requiredType, @Nullable Throwable cause) {
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);
this.value = value;
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");
* you may not use this file except in compliance with the License.
@ -62,7 +62,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, BeansException ex) {
this(resourceDescription, beanName, propertyName, "");
this(resourceDescription, beanName, propertyName, ex.getMessage());
initCause(ex);
}
@ -94,7 +94,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
public UnsatisfiedDependencyException(
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, BeansException ex) {
this(resourceDescription, beanName, injectionPoint, "");
this(resourceDescription, beanName, injectionPoint, ex.getMessage());
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");
* 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);
}
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;
}
else {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
@ -1302,8 +1301,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
@ -1699,8 +1697,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
bw.setPropertyValues(new MutablePropertyValues(deepCopy));
}
catch (BeansException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Error setting property values", ex);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
@ -1752,8 +1749,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
(mbd != null ? mbd.getResourceDescription() : null), beanName, ex.getMessage(), ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

View File

@ -303,8 +303,7 @@ class ConstructorResolver {
return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean instantiation via constructor failed", ex);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
@ -631,8 +630,7 @@ class ConstructorResolver {
mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean instantiation via factory method failed", ex);
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), 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");
* 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);
}
catch (InvocationTargetException ex) {
String msg = "Factory method '" + factoryMethod.getName() + "' threw exception";
String msg = ex.getTargetException().getMessage();
if (bd.getFactoryBeanName() != null && owner instanceof ConfigurableBeanFactory &&
((ConfigurableBeanFactory) owner).isCurrentlyInCreation(bd.getFactoryBeanName())) {
msg = "Circular reference involving containing bean '" + bd.getFactoryBeanName() + "' - consider " +

View File

@ -606,7 +606,7 @@ class ConfigurationClassParser {
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to process import candidates for configuration class [" +
configClass.getMetadata().getClassName() + "]", ex);
configClass.getMetadata().getClassName() + "]: " + ex.getMessage(), ex);
}
finally {
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");
* you may not use this file except in compliance with the License.
@ -155,7 +155,7 @@ public class EventListenerMethodProcessor
}
catch (Throwable ex) {
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");
}
catch (BeanCreationException ex) {
assertThat(ex.toString().contains("touchy")).isTrue();
ex.printStackTrace();
assertThat(ex.toString().contains("touchy")).isTrue();
assertThat((Object) ex.getRelatedCauses()).isNull();
}
}
@ -1370,7 +1370,7 @@ class XmlBeanFactoryTests {
reader.loadBeanDefinitions(INVALID_NO_SUCH_METHOD_CONTEXT);
assertThatExceptionOfType(BeanDefinitionStoreException.class).isThrownBy(() ->
xbf.getBean("constructorOverrides"))
.withMessageContaining("bogusMethod");
.satisfies(ex -> ex.getCause().getMessage().contains("bogusMethod"));
}
@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");
* you may not use this file except in compliance with the License.
@ -56,7 +56,7 @@ public class Spr12278Tests {
public void componentTwoSpecificConstructorsNoHint() {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
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 Juergen Hoeller
* @see #getMessage
* @see #printStackTrace
* @see NestedRuntimeException
*/
public abstract class NestedCheckedException extends Exception {
@ -41,12 +40,6 @@ public abstract class NestedCheckedException extends Exception {
/** Use serialVersionUID from Spring 1.2 for interoperability. */
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.
@ -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.
* @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");
* you may not use this file except in compliance with the License.
@ -29,8 +29,6 @@ import org.springframework.lang.Nullable;
* @since 2.0
* @see NestedRuntimeException
* @see NestedCheckedException
* @see NestedIOException
* @see org.springframework.web.util.NestedServletException
*/
public abstract class NestedExceptionUtils {
@ -39,7 +37,10 @@ public abstract class NestedExceptionUtils {
* @param message the base message
* @param cause the root cause
* @return the full exception message
* @deprecated as of 6.0, in favor of custom exception messages
* with selective inclusion of cause messages
*/
@Deprecated
@Nullable
public static String buildMessage(@Nullable String message, @Nullable Throwable cause) {
if (cause == null) {

View File

@ -33,7 +33,6 @@ import org.springframework.lang.Nullable;
* @author Rod Johnson
* @author Juergen Hoeller
* @see #getMessage
* @see #printStackTrace
* @see NestedCheckedException
*/
public abstract class NestedRuntimeException extends RuntimeException {
@ -41,12 +40,6 @@ public abstract class NestedRuntimeException extends RuntimeException {
/** Use serialVersionUID from Spring 1.2 for interoperability. */
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.
@ -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.
* @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");
* 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);
}
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");
* you may not use this file except in compliance with the License.
@ -105,8 +105,7 @@ public class ConnectionFactoryUtilsUnitTests {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage(
"TASK; SQL [SOME-SQL]; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE");
TransientDataAccessResourceException.class).hasMessage("TASK; SQL [SOME-SQL]; MESSAGE");
}
@Test
@ -114,21 +113,20 @@ public class ConnectionFactoryUtilsUnitTests {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null,
new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage(
"TASK; MESSAGE; nested exception is io.r2dbc.spi.R2dbcTransientResourceException: MESSAGE");
TransientDataAccessResourceException.class).hasMessage("TASK; MESSAGE");
}
@Test
public void messageGenerationNullMessage() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException());
assertThat(exception).isInstanceOf(
TransientDataAccessResourceException.class).hasMessage(
"TASK; SQL [SOME-SQL]; null; nested exception is io.r2dbc.spi.R2dbcTransientResourceException");
TransientDataAccessResourceException.class).hasMessage("TASK; SQL [SOME-SQL]; null");
}
@SuppressWarnings("serial")
private static class MyTransientExceptions extends R2dbcException {
}
}

View File

@ -75,7 +75,7 @@ public abstract class AbstractXmlHttpMessageConverter<T> extends AbstractHttpMes
throw 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);
}
}

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");
* 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) {
throw new HttpMessageNotReadableException(
"Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex, inputMessage);
"Could not unmarshal to [" + elementClass + "]: " + ex, ex, inputMessage);
}
catch (JAXBException 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 org.springframework.core.NestedExceptionUtils;
import org.springframework.core.NestedRuntimeException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.lang.Nullable;
/**
* {@link RuntimeException} that implements {@link ErrorResponse} to expose
* 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
public String getMessage() {
String message = this.status + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
return NestedExceptionUtils.buildMessage(message, getCause());
return this.status + (!this.headers.isEmpty() ? ", headers=" + this.headers : "") + ", " + this.body;
}
}

View File

@ -16,11 +16,12 @@
package org.springframework.web.bind;
import jakarta.servlet.ServletException;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
import org.springframework.web.util.NestedServletException;
/**
* Fatal binding exception, thrown when we want to
@ -34,7 +35,7 @@ import org.springframework.web.util.NestedServletException;
* @author Juergen Hoeller
*/
@SuppressWarnings("serial")
public class ServletRequestBindingException extends NestedServletException implements ErrorResponse {
public class ServletRequestBindingException extends ServletException implements ErrorResponse {
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");
* 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.support.ServletContextResourceLoader;
import org.springframework.web.context.support.StandardServletEnvironment;
import org.springframework.web.util.NestedServletException;
/**
* Simple base implementation of {@link jakarta.servlet.Filter} which treats
@ -230,7 +229,7 @@ public abstract class GenericFilterBean implements Filter, BeanNameAware, Enviro
String msg = "Failed to set bean properties on filter '" +
filterConfig.getFilterName() + "': " + ex.getMessage();
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;
import org.springframework.core.NestedExceptionUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.lang.Nullable;
@ -112,8 +111,7 @@ public class ResponseStatusException extends ErrorResponseException {
@Override
public String getMessage() {
String msg = getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
return NestedExceptionUtils.buildMessage(msg, getCause());
return getStatusCode() + (this.reason != null ? " \"" + this.reason + "\"" : "");
}
}

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

View File

@ -598,8 +598,7 @@ class RestTemplateTests {
assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() ->
template.getForObject(url, String.class))
.withMessage("I/O error on GET request for \"https://example.com/resource\": " +
"Socket failure; nested exception is java.io.IOException: Socket failure");
.withMessage("I/O error on GET request for \"https://example.com/resource\": Socket failure");
}
@Test // SPR-15900
@ -615,8 +614,7 @@ class RestTemplateTests {
assertThatExceptionOfType(ResourceAccessException.class).isThrownBy(() ->
template.getForObject(uri, String.class))
.withMessage("I/O error on GET request for \"https://example.com/resource\": " +
"Socket failure; nested exception is java.io.IOException: Socket failure");
.withMessage("I/O error on GET request for \"https://example.com/resource\": Socket failure");
}
@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");
* you may not use this file except in compliance with the License.
@ -47,7 +47,7 @@ public class Spr8510Tests {
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
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(() ->
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(() ->
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(() ->
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(() ->
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");
* 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.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.util.NestedServletException;
import org.springframework.web.util.ServletRequestPathUtils;
import org.springframework.web.util.WebUtils;
@ -1077,7 +1076,7 @@ public class DispatcherServlet extends FrameworkServlet {
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// 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);
}
@ -1086,7 +1085,7 @@ public class DispatcherServlet extends FrameworkServlet {
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
new ServletException("Handler processing failed: " + err, err));
}
finally {
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");
* 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.XmlWebApplicationContext;
import org.springframework.web.cors.CorsUtils;
import org.springframework.web.util.NestedServletException;
import org.springframework.web.util.WebUtils;
/**
@ -1009,7 +1008,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
throw new ServletException("Request processing failed: " + ex, ex);
}
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");
* 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.util.concurrent.Callable;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
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.ModelAndViewContainer;
import org.springframework.web.servlet.View;
import org.springframework.web.util.NestedServletException;
/**
* Extends {@link InvocableHandlerMethod} with the ability to handle return
@ -220,7 +220,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
throw (Exception) result;
}
else if (result instanceof Throwable) {
throw new NestedServletException("Async processing failed", (Throwable) result);
throw new ServletException("Async processing failed: " + result, (Throwable) result);
}
return result;
}, 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");
* 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.markup.MarkupTemplateEngine;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@ -34,7 +35,6 @@ import org.springframework.context.ApplicationContextException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.AbstractTemplateView;
import org.springframework.web.util.NestedServletException;
/**
* An {@link AbstractTemplateView} subclass based on Groovy XML/XHTML markup templates.
@ -133,7 +133,7 @@ public class GroovyMarkupView extends AbstractTemplateView {
}
catch (ClassNotFoundException 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 '" +
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");
* 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.Locale;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
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.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import org.springframework.web.util.NestedServletException;
import static org.assertj.core.api.Assertions.assertThat;
@ -315,7 +315,7 @@ public class ExceptionHandlerExceptionResolverTests {
AssertionError err = new AssertionError("argh");
HandlerMethod handlerMethod = new HandlerMethod(new ResponseBodyController(), "handle");
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.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");
* you may not use this file except in compliance with the License.
@ -181,7 +181,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
failure = 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 {
if (failure != null) {