This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-engine.git


The following commit(s) were added to refs/heads/master by this push:
     new 48c67bc  SLING-13414 : DefaultErrorHandler: better support non-HTML 
requests (#105)
48c67bc is described below

commit 48c67bc13577194427abe70ac00fae92bae7c85e
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Fri Nov 13 14:37:30 2026 +0100

    SLING-13414 : DefaultErrorHandler: better support non-HTML requests (#105)
    
    * SLING-13414 : DefaultErrorHandler: better support non-HTML requests
    
    Co-authored-by: Copilot <[email protected]>
    
    * SLING-13414 : DefaultErrorHandler: better support non-HTML requests
    
    Co-authored-by: Copilot <[email protected]>
    
    * Do not set null as content type
    
    * Use constants and fix test cases
    
    ---------
    
    Co-authored-by: Copilot <[email protected]>
---
 .../sling/engine/impl/DefaultErrorHandler.java     | 11 +++++++++
 .../sling/engine/impl/DefaultErrorHandlerTest.java | 28 ++++++++++++++++++++++
 .../engine/impl/SlingRequestProcessorImplTest.java |  3 +++
 3 files changed, 42 insertions(+)

diff --git 
a/src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java 
b/src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java
index 1e9192f..a2b669f 100644
--- a/src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java
+++ b/src/main/java/org/apache/sling/engine/impl/DefaultErrorHandler.java
@@ -262,6 +262,17 @@ public class DefaultErrorHandler implements 
JakartaErrorHandler {
             final HttpServletRequest request,
             final HttpServletResponse response)
             throws IOException {
+        // Is html requested?
+        final String contentType =
+                
JakartaMediaRangeList.fromRequest(request).prefer(JakartaMediaRangeList.CONTENT_TYPE_HTML);
+        if (contentType == null) {
+            response.setStatus(status);
+            response.setContentLength(0);
+            // commit the response
+            response.flushBuffer();
+            return;
+        }
+
         // error situation
         final String servletName = (String) 
request.getAttribute(RequestDispatcher.ERROR_SERVLET_NAME);
         String requestURI = (String) 
request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI);
diff --git 
a/src/test/java/org/apache/sling/engine/impl/DefaultErrorHandlerTest.java 
b/src/test/java/org/apache/sling/engine/impl/DefaultErrorHandlerTest.java
index 3d36fc3..6b0fef0 100644
--- a/src/test/java/org/apache/sling/engine/impl/DefaultErrorHandlerTest.java
+++ b/src/test/java/org/apache/sling/engine/impl/DefaultErrorHandlerTest.java
@@ -34,6 +34,7 @@ import org.junit.Test;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
@@ -79,6 +80,7 @@ public class DefaultErrorHandlerTest {
 
     @Test
     public void 
testHandleThrowableWithoutDelegateDoesNotLeakStacktraceOrTracker() throws 
Exception {
+        when(request.getHeader("Accept")).thenReturn("text/html");
         final Exception cause = new IllegalStateException("some internal 
detail: /etc/secret-path");
 
         handler.handleError(cause, request, response);
@@ -106,6 +108,8 @@ public class DefaultErrorHandlerTest {
 
     @Test
     public void testHandleStatusWithoutDelegateStillRendersMessage() throws 
Exception {
+        when(request.getHeader("Accept")).thenReturn("text/html");
+
         handler.handleError(404, "not found", request, response);
 
         verify(response).setStatus(404);
@@ -115,6 +119,30 @@ public class DefaultErrorHandlerTest {
         assertFalse("response body must not contain a server/address footer", 
body.contains("<address>"));
     }
 
+    @Test
+    public void 
testHandleStatusWithoutDelegateForNonHtmlRequestDoesNotRenderBody() throws 
Exception {
+        when(request.getHeader("Accept")).thenReturn("application/json");
+
+        handler.handleError(404, "not found", request, response);
+
+        verify(response).setStatus(404);
+        verify(response).flushBuffer();
+        verify(response, never()).getWriter();
+        assertEquals("", responseBody.toString());
+    }
+
+    @Test
+    public void 
testHandleThrowableWithoutDelegateForNonHtmlRequestDoesNotRenderBody() throws 
Exception {
+        when(request.getHeader("Accept")).thenReturn("text/plain");
+
+        handler.handleError(new IllegalStateException("some internal detail"), 
request, response);
+
+        verify(response).setStatus(500);
+        verify(response).flushBuffer();
+        verify(response, never()).getWriter();
+        assertEquals("", responseBody.toString());
+    }
+
     @Test
     public void testHandleThrowableWithDelegateDoesNotUseFallback() throws 
Exception {
         final org.apache.sling.api.servlets.JakartaErrorHandler delegate =
diff --git 
a/src/test/java/org/apache/sling/engine/impl/SlingRequestProcessorImplTest.java 
b/src/test/java/org/apache/sling/engine/impl/SlingRequestProcessorImplTest.java
index 68be674..e8aa920 100644
--- 
a/src/test/java/org/apache/sling/engine/impl/SlingRequestProcessorImplTest.java
+++ 
b/src/test/java/org/apache/sling/engine/impl/SlingRequestProcessorImplTest.java
@@ -103,6 +103,8 @@ public class SlingRequestProcessorImplTest {
 
     @Test
     public void testHandleErrorWithNullMessageStillSetsStatus() throws 
Exception {
+        when(request.getHeader("Accept")).thenReturn("text/html");
+
         processor.handleError(SC_BAD_REQUEST, null, request, response);
 
         verify(response).setStatus(SC_BAD_REQUEST);
@@ -166,6 +168,7 @@ public class SlingRequestProcessorImplTest {
         when(httpServletRequest.getContextPath()).thenReturn("");
         when(httpServletRequest.getServletPath()).thenReturn("");
         when(httpServletRequest.getMethod()).thenReturn("GET");
+        when(httpServletRequest.getHeader("Accept")).thenReturn("text/html");
 
         final HttpServletResponse httpServletResponse = 
mock(HttpServletResponse.class);
         final StringWriter writer = new StringWriter();

Reply via email to