79322498

Date: 2025-01-01 23:36:34
Score: 0.5
Natty:
Report link

You probably close the stream (try-with-resource) when you throw an exception. Try to define a separate stream and wrap it with StreamingResponseBody and then the exception will be handled correctly @ControllerAdvice.

StreamingResponseBody responseBody = outputStream -> {
    try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        //some logic here
        byteArrayOutputStream.writeTo(outputStream);
    } catch (IOException e) {
        //byteArrayOutputStream will be closed but not outputStream
        log.error("Error occurred while streaming: {}", e.getMessage(), e);
        throw new CustomException("Error due to I/O issues.", e);
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @ControllerAdvice
  • Low reputation (0.5):
Posted by: michalavis