diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCore.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCore.java index b6323ac4554024cfc82171b8ea74924812498d3d..c34001666b0c993dae73df6d792caa8d93e6067a 100644 --- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCore.java +++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCore.java @@ -17,6 +17,8 @@ package org.opengroup.osdu.indexer.error; import javax.validation.ValidationException; import javassist.NotFoundException; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.Ordered; @@ -29,6 +31,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; import org.opengroup.osdu.core.common.model.http.AppException; +import java.io.IOException; + @Order(Ordered.HIGHEST_PRECEDENCE - 1) @ControllerAdvice public class GlobalExceptionMapperCore extends ResponseEntityExceptionHandler { @@ -59,6 +63,17 @@ public class GlobalExceptionMapperCore extends ResponseEntityExceptionHandler { new AppException(HttpStatus.FORBIDDEN.value(), "Access denied", e.getMessage(), e)); } + @ExceptionHandler(IOException.class) + public ResponseEntity<Object> handleIOException(IOException e) { + if (StringUtils.containsIgnoreCase(ExceptionUtils.getRootCauseMessage(e), "Broken pipe")) { + this.logger.warning("Client closed the connection while request still being processed"); + return null; + } else { + return this.getErrorResponse( + new AppException(HttpStatus.SERVICE_UNAVAILABLE.value(), "Unknown error", e.getMessage(), e)); + } + } + @ExceptionHandler(Exception.class) protected ResponseEntity<Object> handleGeneralException(Exception e) { return this.getErrorResponse( diff --git a/indexer-core/src/test/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCoreTest.java b/indexer-core/src/test/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCoreTest.java index aa53ea967f6a2ddfcc7324d46caabdffd82edf5f..992c0a01fea9b1892e20eaaa31b5674028741a85 100644 --- a/indexer-core/src/test/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCoreTest.java +++ b/indexer-core/src/test/java/org/opengroup/osdu/indexer/error/GlobalExceptionMapperCoreTest.java @@ -29,8 +29,9 @@ import org.springframework.security.access.AccessDeniedException; import javax.validation.ValidationException; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import java.io.IOException; + +import static org.junit.Assert.*; @RunWith(PowerMockRunner.class) public class GlobalExceptionMapperCoreTest { @@ -90,4 +91,20 @@ public class GlobalExceptionMapperCoreTest { ResponseEntity<Object> response = sut.handleAppException(exception); assertEquals(RequestStatus.INVALID_RECORD, response.getStatusCodeValue()); } + + @Test + public void should_returnNullResponse_when_BrokenPipeIOExceptionIsCaptured() { + IOException ioException = new IOException("Broken pipe"); + + ResponseEntity response = this.sut.handleIOException(ioException); + assertNull(response); + } + + @Test + public void should_returnServiceUnavailable_when_IOExceptionIsCaptured() { + IOException ioException = new IOException("Not broken yet"); + + ResponseEntity response = this.sut.handleIOException(ioException); + assertEquals(HttpStatus.SC_SERVICE_UNAVAILABLE, response.getStatusCodeValue()); + } } \ No newline at end of file