]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8321 Improve error handling when error is thrown by web service
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 9 Nov 2016 14:38:11 +0000 (15:38 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 9 Nov 2016 14:51:52 +0000 (15:51 +0100)
server/sonar-server/src/main/java/org/sonar/server/ws/WebServiceEngine.java
server/sonar-server/src/test/java/org/sonar/server/ws/WebServiceEngineTest.java

index 3f9a1f3d81a8fb9bd58c3c9954359c55a7329810..85699bad2b803901c5bc2d64dc037c0901f8b3d3 100644 (file)
@@ -25,7 +25,6 @@ import java.util.List;
 import java.util.Locale;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.catalina.connector.ClientAbortException;
 import org.picocontainer.Startable;
 import org.sonar.api.i18n.I18n;
 import org.sonar.api.server.ServerSide;
@@ -114,8 +113,8 @@ public class WebServiceEngine implements LocalConnector, Startable {
     } catch (ServerException e) {
       sendErrors(response, e.httpCode(), new Errors().add(Message.of(e.getMessage())));
     } catch (Exception e) {
-      Throwable cause = e.getCause();
-      if (cause != null && cause instanceof ClientAbortException) {
+      Response.Stream stream = response.stream();
+      if (stream instanceof ServletResponse.ServletStream && ((ServletResponse.ServletStream) stream).response().isCommitted()) {
         // Request has been aborted by the client, nothing can been done as Tomcat has committed the response
         LOGGER.debug("Request {} has been aborted by client, error is '{}'", request, e.getMessage());
         return;
index 4145340826b833e3403103a819aeb129bc72d566..a5a0c1b0cca1e2e45ae51fab826e4c0dd0c8ca3a 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.ws;
 
 import java.io.IOException;
 import java.util.Locale;
+import javax.servlet.http.HttpServletResponse;
 import org.apache.catalina.connector.ClientAbortException;
 import org.apache.commons.io.IOUtils;
 import org.junit.After;
@@ -289,12 +290,16 @@ public class WebServiceEngineTest {
   }
 
   @Test
-  public void does_not_fail_when_request_is_aborted() throws Exception {
+  public void does_not_fail_when_request_is_aborted_and_response_is_committed() throws Exception {
     ValidatingRequest request = new TestRequest().setMethod("GET").setPath("/api/system/fail_with_client_abort_exception");
-    DumbResponse response = new DumbResponse();
+    Response response = mock(Response.class);
+    ServletResponse.ServletStream servletStream = mock(ServletResponse.ServletStream.class);
+    when(response.stream()).thenReturn(servletStream);
+    HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
+    when(httpServletResponse.isCommitted()).thenReturn(true);
+    when(servletStream.response()).thenReturn(httpServletResponse);
     underTest.execute(request, response);
 
-    assertThat(response.stream().outputAsString()).isEmpty();
     assertThat(logTester.logs(LoggerLevel.DEBUG)).isNotEmpty();
   }