0

What i have: AEM 6.4 instance. If somewhere in my code I have RuntimeExcepption error, my page return 200 OK status. To solve this problem, I tried to do Filter.

@SlingFilter( order = Integer.MIN_VALUE, scope = SlingFilterScope.REQUEST)
public class RuntimeExceptionFilter implements javax.servlet.Filter {

    @Override
    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;

        try {
            chain.doFilter(request, response);
        } catch (RuntimeException ex) {
            slingResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }
}

Now I can catch request, which leads to RuntimeException, but slingResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

return me "java.lang.IllegalStateException: Response has already been committed". What can be the reason for it? Or can be here other ways to make page return 500 Status if RuntimeException occurred?

Stacktrace:

Caused by: java.lang.IllegalStateException: Response has already been committed
    at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.checkCommitted(SlingHttpServletResponseImpl.java:424) [org.apache.sling.engine:2.6.16]
    at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.sendError(SlingHttpServletResponseImpl.java:170) [org.apache.sling.engine:2.6.16]
    at org.apache.sling.engine.impl.SlingHttpServletResponseImpl.sendError(SlingHttpServletResponseImpl.java:165) [org.apache.sling.engine:2.6.16]
    at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:127) [org.apache.felix.http.servlet-api:1.1.2]
    at my.project.RuntimeExceptionFilter.doFilter(RuntimeExceptionFilter.java:49)

1 Answers1

0

As the exception text says, the response was already sent prior your exception being thrown. Similar question was discussed here: Response has aready been committed

First try to find at which point exactly is the HTTP response being returned. Once you find that you can work your way around it.

I'm not very familiar with AEM internal (had very little hand-on experience with it) but most likely there is some wrapper (Aspect maybe) that get's triggered at some point and returns the HTTP response with 200.

SasaFajkovic
  • 395
  • 1
  • 4
  • 16