When sending a request to a resource with an invalid range, the server responds with a 500 Internal server error instead of 416 Requested range not satisfiable.

When the ResourceHttpRequestHandler is executed with an invalid range then it will call the HttpServletResponse.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE) method: https://github.com/spring-projects/spring-framework/blob/75329e6d9d93180d9edceb81838b0d96a145d665/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java#L578-L581 Further down the processing line this will not result in a ProblemDetail body response, so the writeWithMessageConverters method in AbstractMessageConverterMethodProcessor will throw a HttpMessageNotWritableException from: https://github.com/spring-projects/spring-framework/blob/8b14bf86efd590247b2b6fd4843023ee283768d0/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java#L364-L367 (Where valueType is java.util.LinkedHashMap and contentType is multipart/form-data)

Comment From: NielsCW

An example that uses spring-content containing a WebTestClient test that reproduces the issue ~demo-api.zip~

EDIT: Please use iss-34490.zip

Comment From: rstoyanchev

The demo project is missing the (Gradle) build file. Could you provide that as well?

Comment From: NielsCW

Sure, here is the updated demo-api.zip

Comment From: rstoyanchev

There is still no build file in the zip.

Comment From: NielsCW

The first one did indeed not contain a build.gradle file, here is a renamed version of the second zip with build.gradle to avoid confusion:

iss-34490.zip

Comment From: bclozel

Sorry for the delayed response. After reproducing the problem, I can confirm that the following happens: * the invalid range is parsed and an IllegalArgumentException is thrown * this exception is caught in the resource handler and it sets the relevant range header and uses sendError with the 416 response status * the error dispatch reaches the error handling processing in Spring Boot * error attributes are collected and ready to be written to the response * because the content type of the response is already set to "multipart/form-data", the error metadata cannot be written to the response because no message converter knows how to handle that * As a result, we get an HTTP 500 error response

I think this issue is quite similar to #34366 and we should apply the same fix: reset the content-type response header and let the error handling deal with the response. With that fix in place, you sample will produce the following:

< 416 REQUESTED_RANGE_NOT_SATISFIABLE Requested range not satisfiable
< Vary: [Origin, Access-Control-Request-Method, Access-Control-Request-Headers]
< Last-Modified: [Sun, 15 Jun 2025 16:41:18 GMT]
< Content-Disposition: [attachment; filename="=?UTF-8?Q?hello.txt?="; filename*=UTF-8''hello.txt]
< Accept-Ranges: [bytes]
< Content-Range: [bytes */12]
< Content-Type: [application/json]
< Transfer-Encoding: [chunked]
< Date: [Sun, 15 Jun 2025 16:41:18 GMT]

{"timestamp":"2025-06-15T16:41:18.232+00:00","status":416,"error":"Requested range not satisfiable","path":"/foos/07cb8def-84d5-4374-9bdb-139606a73e06/file"}

Comment From: bclozel

Closed with faada70d592b31595c389