Commit 23b5b349 authored by neelesh thakur's avatar neelesh thakur
Browse files

broken pipe IOException handler

parent 0ce04afe
Pipeline #66432 failed with stages
in 46 minutes and 40 seconds
......@@ -19,6 +19,8 @@ import javax.validation.ValidationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
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;
......@@ -35,6 +37,8 @@ import org.springframework.web.context.request.WebRequest;
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)
@ControllerAdvice
public class GlobalExceptionMapper extends ResponseEntityExceptionHandler {
......@@ -77,6 +81,17 @@ public class GlobalExceptionMapper 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));
}
}
@Override
@NonNull
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(@NonNull HttpRequestMethodNotSupportedException e,
......
......@@ -16,6 +16,7 @@ package org.opengroup.osdu.storage.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.verify;
import javax.validation.ValidationException;
......@@ -36,6 +37,8 @@ import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.springframework.http.ResponseEntity;
import org.mockito.runners.MockitoJUnitRunner;
import java.io.IOException;
@RunWith(MockitoJUnitRunner.class)
public class GlobalExceptionMapperTest {
......@@ -109,4 +112,22 @@ public class GlobalExceptionMapperTest {
assertEquals(AppError.class, response.getBody().getClass());
assertEquals("Unrecognized property.", ((AppError)response.getBody()).getReason());
}
@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
Supports Markdown
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