ResponseEntity<StreamingResponseBody> Exception Handling Issues

See original GitHub issue

Summary

  • When using @ExceptionHandler (or @ControllerAdvice), exceptions thrown while writing to a StreamingResponseBody are not handled correctly (or at least as I expect them to be…) when the request mapping returns ResponseEntity<StreamingResponseBody>
  • Exception handling does work as expected when the return type is just StreamingResponseBody
  • It’s possible my understanding of how these should work is incorrect, however I haven’t seen any evidence yet to point to this

Description

  • If a runtime exception occurs while writing to the StreamingResponseBody wrapped in a ResponseEntity the endpoint does invoke the @ExceptionHandler but returns a 500 status with no response body no matter what
  • If the request mapping returns a StreamingResponseBody directly, error handling does work as expected, and the response code and response body are correct
  • While writing tests for this, I can’t get the tests to return anything other than 200 status. While running tests, I also don’t see the exception handlers getting invoked. I’m not sure if this is another bug or if my test setup is incorrect. If you hit the endpoints directly in the browser or via CURL, you do see the 500 status with no body. This is my first time using StreamingResponseBody so I apologize if I’m just doing things incorrectly
  • Sample project to reproduce: https://github.com/austinarbor/spring-boot-streaming-response-issue

Misc

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vforchicommented, Nov 25, 2020

I have a similar issue: the application works as expected, no matter if I wrap the StreamingResponseBody in a ResponseEntity or not, but in the tests I always get a 200 and an empty body. I tried with @WebMvcTest+mockMvc and with @SpringBootTest+TestRestTemplate: same behaviour.

0reactions
rstoyanchevcommented, Dec 3, 2020

@austinarbor yes that would be the workaround for the @ExceptionHandler method. Also the test updates look about right from a quick look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

spring - How to handle Exception occuring when returning ...
I have implemented a Spring Rest Controller that streams back large files using the StreamingResponseBody. However, these files are coming ...
Read more >
Exception handling in StreamingResponseBody-Spring MVC
The best way I discovered to handle errors/exceptions in the web environment is to create your custom exception with the disabled stack trace,...
Read more >
spring-projects/spring-boot - Gitter
Anyone knows how to handle exceptions when you are using ... Maybe in this case you could return a ResponseEntity (and a StreamingResponseBody...
Read more >
Streaming large dataset over Rest? - jvm aware
This short article will explore a few options to handle checked and unchecked exceptions in lambda expressions. Lambda Expressions and Exception ...
Read more >
22. Web MVC framework - Spring
Handler exception resolvers that are declared in the ... Note that StreamingResponseBody can also be used as the body in a ResponseEntity in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found