0

I have a Spring REST endpoint that downloads a zip archive using StreamingResponseBody:

@GetMapping("/zip/download")
public ResponseEntity<StreamingResponseBody> downloadZip(
        @RequestParam String id) {
  return zipService.downloadZip(id);
}

When downloading large files, there is a possibility of an exception, and if an exception occurs, the browser doesn't know anything about it, and I just get an inferior, broken zip archive, and the user knows nothing about it.

How to tell the browser that an error has occurred so that it displays the file as an error when downloading? I expect the browser to write in the download list that an error has occurred, for example: screenshot. But download is shown as completed successfully and i get wrong file.

I tried write ExceptionHandler like this:

@ExceptionHandler(ZipArchiveDownloadException.class)
ResponseEntity<ApiError> handleZipArchiveDownloadException(ZipArchiveDownloadException e, HttpServletRequest request, HttpServletResponse response) {
  ApiError apiError = ApiError.of("Internal Server Error", e.getLocalizedMessage());
  response.setContentType("application/json");
  return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
          .contentType(MediaType.APPLICATION_JSON)
          .body(apiError);
}

But it didn't help and also i get exceptions:

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.test.exception..ApiError] with preset Content-Type 'application/zip'
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:309) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:219) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:123) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:407) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:61) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1300) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1111) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
2022-11-25 10:32:56.676 ERROR 43440 --- [0.0-9001-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/zip/download] and exception [] as the response has already been committed. As a result, the response may have the wrong status code.

What do I need to do to make the browser recognize the error and display the file as a download error?

vmelik
  • 1

0 Answers0