wimdeblauwe / error-handling-spring-boot-starter

Spring Boot starter for configurable REST API error handling
https://wimdeblauwe.github.io/error-handling-spring-boot-starter/
Apache License 2.0
430 stars 52 forks source link

Handling Client Disconnects and IOException in Image Delivery Process #82

Closed calism2 closed 10 months ago

calism2 commented 10 months ago

Hello,

I'm currently working on the image delivery process for our clients. In the event of a client disconnect without receiving the image, Spring triggers an exception (org.apache.catalina.connector.ClientAbortException). To manage this, our error handling mechanism intercepts the exception and attempts to return an (ApiErrorResponse) value.

However, a challenge arises as the socket is already closed, leading to Tomcat throwing an IOException when trying to write to the closed socket.

I've addressed this issue with the following solution ( If anyone needed ). Do you have any suggestions for a more appropriate resolution, or is this approach the recommended way to address the problem?

Thank you.

@ControllerAdvice
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class FallbackExceptionHandler extends ErrorHandlingControllerAdvice {

    public FallbackExceptionHandler(List<ApiExceptionHandler> handlers, FallbackApiExceptionHandler fallbackHandler, LoggingService loggingService, List<ApiErrorResponseCustomizer> responseCustomizers) {
        super(handlers, fallbackHandler, loggingService, responseCustomizers);
    }

    @ExceptionHandler
    public void handleException(ClientAbortException exception) {
        // do nothing
    }
}
wimdeblauwe commented 10 months ago

If it works for you, I think your solution is fine.