001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.util; 018 019import javax.servlet.ServletException; 020 021import org.springframework.core.NestedExceptionUtils; 022import org.springframework.lang.Nullable; 023 024/** 025 * Subclass of {@link ServletException} that properly handles a root cause in terms 026 * of message and stacktrace, just like NestedChecked/RuntimeException does. 027 * 028 * <p>Note that the plain ServletException doesn't expose its root cause at all, 029 * neither in the exception message nor in printed stack traces! While this might 030 * be fixed in later Servlet API variants (which even differ per vendor for the 031 * same API version), it is not reliably available on Servlet 2.4 (the minimum 032 * version required by Spring 3.x), which is why we need to do it ourselves. 033 * 034 * <p>The similarity between this class and the NestedChecked/RuntimeException 035 * class is unavoidable, as this class needs to derive from ServletException. 036 * 037 * @author Juergen Hoeller 038 * @since 1.2.5 039 * @see #getMessage 040 * @see #printStackTrace 041 * @see org.springframework.core.NestedCheckedException 042 * @see org.springframework.core.NestedRuntimeException 043 */ 044public class NestedServletException extends ServletException { 045 046 /** Use serialVersionUID from Spring 1.2 for interoperability. */ 047 private static final long serialVersionUID = -5292377985529381145L; 048 049 static { 050 // Eagerly load the NestedExceptionUtils class to avoid classloader deadlock 051 // issues on OSGi when calling getMessage(). Reported by Don Brown; SPR-5607. 052 NestedExceptionUtils.class.getName(); 053 } 054 055 056 /** 057 * Construct a {@code NestedServletException} with the specified detail message. 058 * @param msg the detail message 059 */ 060 public NestedServletException(String msg) { 061 super(msg); 062 } 063 064 /** 065 * Construct a {@code NestedServletException} with the specified detail message 066 * and nested exception. 067 * @param msg the detail message 068 * @param cause the nested exception 069 */ 070 public NestedServletException(@Nullable String msg, @Nullable Throwable cause) { 071 super(msg, cause); 072 } 073 074 075 /** 076 * Return the detail message, including the message from the nested exception 077 * if there is one. 078 */ 079 @Override 080 @Nullable 081 public String getMessage() { 082 return NestedExceptionUtils.buildMessage(super.getMessage(), getCause()); 083 } 084 085}