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

@@ -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
@@ -228,9 +227,9 @@ public abstract class GenericFilterBean implements Filter, BeanNameAware, Enviro
}
catch (BeansException ex) {
String msg = "Failed to set bean properties on filter '" +
filterConfig.getFilterName() + "': " + ex.getMessage();
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);
}
}