-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Common exceptions for resource not found, redirection, noContent etc... [SPR-11552] #16177
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
Comments
Rossen Stoyanchev commented
We have
How do you use this, given that a controller can return a "redirect:" prefixed String.
Also a question mark here. I don't think of this as an error but rather a normal condition covered by What others do you have in mind? |
cemo koc commented I am using org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler with a ControllerAdvice. My controllers are calling a service method.Then a service method is calling another service etc... For example: ControllerA --> ServiceA --> ServiceB In ServiceB I would like to call a RedirectException and want to be handled correctly. This helps to reduce unnecessary return statements and simplify code. Shortly these exceptions will be useful in not only controller layer but also in business layers. It is not easy to returning a ResponseEntity<Void> in business layer. But throwing an exception is easy to deal. Another example will be in method parameter handling. In our code base we are resolving some parameters by URL. If there is mismatch or moved URL, we are throwing an exception to redirect. This is greatly reducing complexity and lets developer to focus on core. A piece of code from my application:
I would like to see these exceptions in Spring Core and handled correctly in both DefaultHandlerExceptionResolver and ResponseEntityExceptionHandler. |
Rossen Stoyanchev commented Business components arguably shouldn't raise exceptions that extend from web layer (or Spring MVC) types. I came across an example in this spring.io PR where a A good way to think about it is how it would be if services were exposed through a different layer (integration scenario, jms, etc). You'd probably wish those exceptions had not been dependent on web concepts. |
cemo koc commented I am considering DefaultHandlerExceptionResolver and ResponseEntityExceptionHandler as a part of web layer. If those exceptions could be declared in Spring MVC, I can not see how different modules will utilize them without depending on them. Referred pull request is very nice and semantically it is very clear. But let me to give another an updated version of this.
This is semantically same as before but reduce necessary codes to be written. You do not need to an exception handling mechanism in BlogController. Consider another Controller similar to BlogController. Do you think that It should also write same handling strategy? |
Rossen Stoyanchev commented Yes I realize that's what you were asking for. However, the BlogService is part of sagan-common which does not depend on Spring MVC. The actual web logic is in sagan-site (see here). Also notice that the BlogController prepends "/blog/" and this is definitely not something that a business component should know.
Yes. If the case with BlogPostMovedException was repeated in sagan in more areas and I found the number of exception-handling methods overwhelming, I might consider an interface (e.g. |
cemo koc opened SPR-11552 and commented
In our codebase we are using some convenient exceptions for resource not found, redirection (302/301) and noContent...
It would be nice to have such kind of exceptions in spring. :)
Edit: I forgot to say that we are providing necessary mapping as well. For example a ResourceNotFoundException returns a 404 to client.
Affects: 3.2.8, 4.0.2
The text was updated successfully, but these errors were encountered: