Commit d506f998 authored by Sanjeev-SLB's avatar Sanjeev-SLB
Browse files

Fixed broken pipe IOException

parent b785e7d0
Pipeline #70136 failed with stages
in 27 minutes and 14 seconds
......@@ -20,6 +20,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.google.gson.Gson;
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.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -37,6 +39,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.io.IOException;
import javax.validation.ValidationException;
import java.util.List;
import java.util.stream.Collectors;
......@@ -105,7 +108,19 @@ public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
protected ResponseEntity<Object> handleAccessDeniedException(AccessDeniedException e) {
return this.getErrorResponse(
new AppException(UNAUTHORIZED.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) { e.printStackTrace(); return
......
......@@ -31,8 +31,11 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.context.request.WebRequest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.powermock.api.mockito.PowerMockito.mock;
import java.io.IOException;
@RunWith(MockitoJUnitRunner.class)
public class GlobalExceptionMapperTest {
@Mock
......@@ -101,4 +104,22 @@ public class GlobalExceptionMapperTest {
* assertEquals(500, 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(503, response.getStatusCodeValue());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment