Skip to content
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

WebMvc.fn: ServerResponse.async does not handle exceptions as expected #25931

Closed
jonasbark opened this issue Oct 19, 2020 · 0 comments
Closed
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@jonasbark
Copy link

Affects:
v5.3.0-RC1

Related Kotlin Slack conversation:
https://kotlinlang.slack.com/archives/C0B8ZTWE4/p1602780417061400

When using functional routing and MVC the exception handling when using ServerResponse.async does not work as expected:

Router:

@Bean
    fun route(
        categoryHandler: TestHandler,
    ): RouterFunction<ServerResponse> = router {
        "/categories".nest {
           GET("/{categoryId}", categoryHandler::categoriesCategoryIdGet)
        }
    }

Implementation:

 fun categoriesCategoryIdGet(request: ServerRequest): ServerResponse {

         return ServerResponse.async(mono(Dispatchers.Unconfined) {
            throw ResponseStatusException(HttpStatus.BAD_GATEWAY)
        })
    }

I would expect the request to show HTTP Bad Gateway, but instead I'm getting HTTP 200 with an empty response:

 curl -v 'http://localhost:8080/categories/1/'
...
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Length: 0
< Date: Mon, 19 Oct 2020 06:33:06 GMT
<
* Connection #0 to host localhost left intact
* Closing connection 0

with the following exception in the server:

javax.servlet.ServletException: javax.servlet.ServletException: org.springframework.web.server.ResponseStatusException: 502 BAD_GATEWAY
	at org.springframework.web.servlet.function.ErrorHandlingServerResponse.handleError(ErrorHandlingServerResponse.java:69) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.function.AsyncServerResponse.lambda$writeTo$0(AsyncServerResponse.java:116) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760) ~[na:1.8.0_201]
	at java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:778) ~[na:1.8.0_201]
	at java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2140) ~[na:1.8.0_201]
	at org.springframework.web.servlet.function.AsyncServerResponse.writeTo(AsyncServerResponse.java:103) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.function.support.HandlerFunctionAdapter.handle(HandlerFunctionAdapter.java:77) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1061) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:961) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.0-RC2.jar:5.3.0-RC2]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.39.jar:4.0.FR]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.39.jar:9.0.39]

I would expect the request to be handled by e.g. ResponseStatusExceptionHandler and the ModelAndView exception handler.
I did not find documentation about the async handling.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 19, 2020
@sdeleuze sdeleuze self-assigned this Oct 19, 2020
@sdeleuze sdeleuze added this to the 5.3.1 milestone Oct 28, 2020
@sdeleuze sdeleuze added in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 28, 2020
@sdeleuze sdeleuze removed their assignment Oct 30, 2020
@poutsma poutsma self-assigned this Oct 30, 2020
@poutsma poutsma modified the milestones: 5.3.1, 5.3.2 Nov 9, 2020
rstoyanchev added a commit that referenced this issue Nov 23, 2020
Now that we have two adapters that need access to the configurer, we need
to save it in a field like others.

See gh-25931
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants