How to catch and Ignore org.apache.catalina.connector.ClientAbortException
Asked Answered
L

3

24

I want to catch and ignore the following tomcat ClientAbortException. As it is unnecessary to pay any attention on this for my program.

Any idea how and where I can catch this exception? Below is the Exception:

14:46:43.920 [ajp-bio-8029-exec-538] ERROR com.sok.runway.URLFilter - Exception in URLFilter
org.apache.catalina.connector.ClientAbortException: null
        at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:388) ~[catalina.jar:7.0.26]
        at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:462) ~[tomcat-coyote.jar:7.0.26]
        at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366) ~[tomcat-coyote.jar:7.0.26]
        at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:413) ~[catalina.jar:7.0.26]
        at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:401) ~[catalina.jar:7.0.26]
        at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91) ~[catalina.jar:7.0.26]
        at org.apache.catalina.servlets.DefaultServlet.copyRange(DefaultServlet.java:2031) ~[catalina.jar:7.0.26]
        at org.apache.catalina.servlets.DefaultServlet.copy(DefaultServlet.java:1870) ~[catalina.jar:7.0.26]
        at org.apache.catalina.servlets.DefaultServlet.serveResource(DefaultServlet.java:958) ~[catalina.jar:7.0.26]
        at org.apache.catalina.servlets.DefaultServlet.doGet(DefaultServlet.java:411) ~[catalina.jar:7.0.26]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) ~[servlet-api.jar:na]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) ~[servlet-api.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [catalina.jar:7.0.26]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.26]
        at com.sok.runway.URLFilter.doFilter(URLFilter.java:130) ~[runway-dev.jar:na]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.26]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) [catalina.jar:7.0.26]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.26]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [catalina.jar:7.0.26]
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) [catalina.jar:7.0.26]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.26]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.26]
        at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:200) [tomcat-coyote.jar:7.0.26]
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) [tomcat-coyote.jar:7.0.26]
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307) [tomcat-coyote.jar:7.0.26]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) [na:1.7.0_03]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) [na:1.7.0_03]
        at java.lang.Thread.run(Thread.java:722) [na:1.7.0_03]
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113) ~[na:1.7.0_03]
        at java.net.SocketOutputStream.write(SocketOutputStream.java:153) ~[na:1.7.0_03]
        at org.apache.coyote.ajp.AjpProcessor.output(AjpProcessor.java:295) [tomcat-coyote.jar:7.0.26]
        at org.apache.coyote.ajp.AbstractAjpProcessor$SocketOutputBuffer.doWrite(AbstractAjpProcessor.java:1082) ~[tomcat-coyote.jar:7.0.26]
        at org.apache.coyote.Response.doWrite(Response.java:533) ~[tomcat-coyote.jar:7.0.26]
        at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:383) ~[catalina.jar:7.0.26]
Leekgreen answered 11/12, 2013 at 3:55 Comment(0)
H
19

since you probably don't want a dependency to Catalina, you can ignore the Exception like this (I'm assuming the ClientAbortException is the cause):

String simpleName = e.getCause().getClass().getSimpleName();
if (simpleName.equals("ClientAbortException")) {
    // ignore
} else {
    // do whatever you do
}
Hoang answered 13/11, 2014 at 7:42 Comment(0)
B
17

In Spring 4, you would put this in a class annotated with @ControllerAdvice. The trick is to only suppress ClientAbortException while allowing every other IOException to be handled as errors or logged, without introducing a compile/runtime dependency on your container API:

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

//import org.apache.catalina.connector.ClientAbortException; // We don't want this dependency.
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public final class ErrorController
{
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handleNoHandlerFoundException(final NoHandlerFoundException ex)
    {
        return "notFoundPage";
    }

    @ExceptionHandler(IOException.class)
    public String handleAbortedConnection(final IOException ex)
    {
        // avoids compile/runtime dependency by using class name
        if (ex.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException"))
        {
            return null;
        }

        // log and return error page for any other IOExceptions
        if (LOGGER.isLoggable(Level.WARNING))
        {
            LOGGER.log(Level.WARNING, "IO Error", ex);
        }

        return "errorPage";
    }

    @ExceptionHandler(Exception.class)
    public String handleException(final Exception ex)
    {
        if (LOGGER.isLoggable(Level.SEVERE))
        {
            LOGGER.log(Level.SEVERE, "Server Error", ex);
        }
        return "errorPage";
    }
}

Thanks to this answer for providing the missing piece where we return null for the view name to avoid throwing a SocketException on the closed OutputStream.

Buckeye answered 18/9, 2017 at 18:56 Comment(0)
C
1

ClientAbortException extends IOException. You can try the code below to catch the exception:

try {  
    // codes
    out.write("xyz"); // throws IOException
} catch (org.apache.catalina.connector.ClientAbortException ca) {
    System.out.println("ClientAbortException caught");
} catch (IOException ioe) {
    System.out.println("i/o exception raised. abrorting.");
}
Crystalcrystalline answered 11/12, 2013 at 6:0 Comment(3)
But my question more like where should i catch this exception in the code? In which class i need to add this catch block?Leekgreen
for that you need to post your codes,and try debugging your code so that you can know exactly at which point you are getting the exception,and then try putting that codes inside try catch.Crystalcrystalline
If do this way, then you will meet compile/runtime dependency issue.Lightship

© 2022 - 2024 — McMap. All rights reserved.