-
Notifications
You must be signed in to change notification settings - Fork 38.5k
SPR-14818: Added MissingHeaderException type #1653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2018 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 | ||
* | ||
* http://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.web.bind; | ||
|
||
import org.springframework.core.MethodParameter; | ||
|
||
/** | ||
* {@link ServletRequestBindingException} subclass that indicates that a header | ||
* variable expected in the method parameters of an {@code @RequestMapping} | ||
* is not present. | ||
* | ||
* @author Per Böckman | ||
* | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class MissingHeaderException extends ServletRequestBindingException { | ||
|
||
private final String name; | ||
|
||
private final MethodParameter parameter; | ||
|
||
|
||
/** | ||
* Constructor for MissingHeaderException. | ||
* @param name the name of the missing header | ||
* @param parameter the method parameter | ||
*/ | ||
public MissingHeaderException(String name, MethodParameter parameter) { | ||
super(""); | ||
this.name = name; | ||
this.parameter = parameter; | ||
} | ||
|
||
|
||
@Override | ||
public String getMessage() { | ||
return "Missing request header '" + name + | ||
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName(); | ||
} | ||
|
||
/** | ||
* Return the expected name of the header. | ||
*/ | ||
public final String getName() { | ||
return this.name; | ||
} | ||
|
||
/** | ||
* Return the method parameter bound to the path variable. | ||
*/ | ||
public final MethodParameter getParameter() { | ||
return this.parameter; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import org.springframework.core.MethodParameter; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.bind.MissingHeaderException; | ||
import org.springframework.web.bind.ServletRequestBindingException; | ||
import org.springframework.web.bind.WebDataBinder; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
|
@@ -82,8 +83,7 @@ protected Object resolveName(String name, MethodParameter parameter, NativeWebRe | |
|
||
@Override | ||
protected void handleMissingValue(String name, MethodParameter parameter) throws ServletRequestBindingException { | ||
throw new ServletRequestBindingException("Missing request header '" + name + | ||
"' for method parameter of type " + parameter.getNestedParameterType().getSimpleName()); | ||
throw new MissingHeaderException(name, parameter); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my humble opinion, this is fine as it used to be, as including a new class is adding also complexity. So, what are the advantages adding this exception class just to manage this one? But this is my opinion!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree with the added complexity. I found https://jira.spring.io/browse/SPR-14818 when trying to return a custom error message for missing headers in the ResponseEntityExceptionHandler#handleServletRequestBindingException callback. Now my workaround is to check the message text of the ServletRequestBindingException for the text "Missing request header".. is there a better way to implement the ResponseEntityExceptionHandler callback? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you mean, they throw more specific exception, more meaningful. I guess it is alright, let see if someone else give their opinion. So far, I don't have anything better from the top of my head!! ;) good work! |
||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right place to sit the exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know but the package contains similar exceptions like MissingPathVariableException