With #19204, explicit handling of FileSizeLimitExceededException was added alongside SizeLimitExceededException. This should be extended to handle FileCountLimitExceededException as well.
Currently, requests that exceed the configured size limit are handled by Spring, and an HTTP 413 Content Too Large response is returned to the client.
However, when a multipart request exceeds the maximum number of parts, a FileCountLimitExceededException is thrown. This exception is not handled by Spring, which ultimately results in an HTTP 500 Internal Server Error response. This does not only cause confusion for clients, but also triggers monitoring/alerting systems that are configured to detect server errors.
To fix this inconsistency, FileCountLimitExceededException should be explicitly caught and handled.
My current workaround is to add the following method to a @ControllerAdvice class of my application:
@ExceptionHandler(FileCountLimitExceededException.class)
@ResponseStatus(HttpStatus.CONTENT_TOO_LARGE)
public void handleFileCountLimitExceededException() {}