Skip to content
Snippets Groups Projects
Commit 646968b7 authored by Sanjeev-SLB's avatar Sanjeev-SLB
Browse files

Broken pipe IOException handler

parent ac64cbf9
No related branches found
No related tags found
1 merge request!214Broken pipe IOException handler
Pipeline #70122 failed
......@@ -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(
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment