diff --git a/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java b/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java index ca18568fcfc31acb919cfba0fc29b339b6e87acf..1b27d7759715afbbc0fdbc8f012573982a373de1 100644 --- a/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java +++ b/legal-core/src/main/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapper.java @@ -20,6 +20,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.gson.Gson; + +import java.io.IOException; import java.util.ArrayList; import java.util.List; import javassist.NotFoundException; @@ -27,6 +29,9 @@ import javax.inject.Inject; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; import javax.validation.ValidationException; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.opengroup.osdu.core.common.logging.JaxRsDpsLog; import org.opengroup.osdu.core.common.model.http.AppException; import org.springframework.core.Ordered; @@ -106,6 +111,17 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler { return this.getErrorResponse(new AppException(HttpStatus.BAD_REQUEST.value(), "Validation error.", result.toString())); } + @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/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java b/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java index a53c8009d56a9dfd608aad2354aa1765a6808caf..5e79afda261ae31bc495f14143ecebb41c23d854 100644 --- a/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java +++ b/legal-core/src/test/java/org/opengroup/osdu/legal/middleware/GlobalExceptionMapperTests.java @@ -1,6 +1,7 @@ package org.opengroup.osdu.legal.middleware; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.powermock.api.mockito.PowerMockito.mock; import javassist.NotFoundException; @@ -19,6 +20,8 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.context.request.WebRequest; +import java.io.IOException; + @RunWith(MockitoJUnitRunner.class) public class GlobalExceptionMapperTests { @Mock @@ -88,5 +91,22 @@ public class GlobalExceptionMapperTests { //assertEquals("An unknown error has occurred.", response.getBody().getMessage()); } + @Test + public void should_returnNullResponse_when_BrokenPipeIOExceptionIsCaptured() { + IOException ioException = new IOException("Broken pipe"); + + ResponseEntity response = sut.handleIOException(ioException); + + assertNull(response); + } + + @Test + public void should_returnServiceUnavailable_when_IOExceptionIsCaptured() { + IOException ioException = new IOException("Not broken yet"); + + ResponseEntity response = sut.handleIOException(ioException); + + assertEquals(org.apache.http.HttpStatus.SC_SERVICE_UNAVAILABLE, response.getStatusCodeValue()); + } } \ No newline at end of file