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.server; 018 019import java.lang.reflect.Method; 020 021import org.springframework.core.MethodParameter; 022import org.springframework.http.HttpStatus; 023import org.springframework.lang.Nullable; 024 025/** 026 * Exception for an {@link HttpStatus#INTERNAL_SERVER_ERROR} that exposes extra 027 * information about a controller method that failed, or a controller method 028 * argument that could not be resolved. 029 * 030 * @author Rossen Stoyanchev 031 * @since 5.0 032 */ 033@SuppressWarnings("serial") 034public class ServerErrorException extends ResponseStatusException { 035 036 @Nullable 037 private final Method handlerMethod; 038 039 @Nullable 040 private final MethodParameter parameter; 041 042 043 /** 044 * Constructor for a 500 error with a reason and an optional cause. 045 * @since 5.0.5 046 */ 047 public ServerErrorException(String reason, @Nullable Throwable cause) { 048 super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); 049 this.handlerMethod = null; 050 this.parameter = null; 051 } 052 053 /** 054 * Constructor for a 500 error with a handler {@link Method} and an optional cause. 055 * @since 5.0.5 056 */ 057 public ServerErrorException(String reason, Method handlerMethod, @Nullable Throwable cause) { 058 super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); 059 this.handlerMethod = handlerMethod; 060 this.parameter = null; 061 } 062 063 /** 064 * Constructor for a 500 error with a {@link MethodParameter} and an optional cause. 065 */ 066 public ServerErrorException(String reason, MethodParameter parameter, @Nullable Throwable cause) { 067 super(HttpStatus.INTERNAL_SERVER_ERROR, reason, cause); 068 this.handlerMethod = parameter.getMethod(); 069 this.parameter = parameter; 070 } 071 072 /** 073 * Constructor for a 500 error linked to a specific {@code MethodParameter}. 074 * @deprecated in favor of {@link #ServerErrorException(String, MethodParameter, Throwable)} 075 */ 076 @Deprecated 077 public ServerErrorException(String reason, MethodParameter parameter) { 078 this(reason, parameter, null); 079 } 080 081 /** 082 * Constructor for a 500 error with a reason only. 083 * @deprecated in favor of {@link #ServerErrorException(String, Throwable)} 084 */ 085 @Deprecated 086 public ServerErrorException(String reason) { 087 super(HttpStatus.INTERNAL_SERVER_ERROR, reason, null); 088 this.handlerMethod = null; 089 this.parameter = null; 090 } 091 092 093 /** 094 * Return the handler method associated with the error, if any. 095 * @since 5.0.5 096 */ 097 @Nullable 098 public Method getHandlerMethod() { 099 return this.handlerMethod; 100 } 101 102 /** 103 * Return the specific method parameter associated with the error, if any. 104 */ 105 @Nullable 106 public MethodParameter getMethodParameter() { 107 return this.parameter; 108 } 109 110}