problem-spring-web icon indicating copy to clipboard operation
problem-spring-web copied to clipboard

[Question/Problem] Cannot capture io.netty.channel.StacklessClosedChannelException

Open SimoneGiusso opened this issue 1 year ago • 0 comments

I have a Spring WebFlux application where sometimes the client closed the connection while the app is writing the response. This cause a io.netty.channel.StacklessClosedChannelException. From the log I see that this is logged as an ERROR by the AdviceTraits.java.

Since I do not want to see this error in the log (which triggers an alert) because is not an application error, it's just a client behaviour that I cannot avoid, I'd like to skip/capture the exception.

I tried to create a custom Trait:

public interface ClosedChannelExceptionAdviceTrait extends AdviceTrait {

    @ExceptionHandler
    default Mono<Void> handleClosedChannel(
        final ServerWebExchange request,
        final ClosedChannelException exception
    ) {
        return Mono.fromRunnable(() -> AdviceTraits.log(exception, BAD_REQUEST));
    }

}

Which is then implemented by my ExceptionHandling:

@ControllerAdvice
class ExceptionHandling implements ProblemHandling, SecurityAdviceTrait, ClosedChannelExceptionAdviceTrait {
    // some code...
}

The problem is that nothing changed, the exception is still logged as an error by the AdviceTraits.

I had the same problem with Spring MVC (here I'm not using this library though) and I resolved by just having this method in my @ControllerAdvice:

    @ExceptionHandler(ClientAbortException.class) // the exception is different because the web engine is not netty
    private void clientAbortException() {
        log.warn("Client closed connection!");
    }

And it works.

While I was able to replicate the error locally in Spring MVC, I didn't find a way to replicate it for Spring WebFlux.

Is there a way to solve this problem without modifying the AdviceTraits.java class (which I cannot do)?

SimoneGiusso avatar May 17 '23 08:05 SimoneGiusso