aboutsummaryrefslogtreecommitdiffstats
path: root/workflow-designer-be/src
diff options
context:
space:
mode:
Diffstat (limited to 'workflow-designer-be/src')
-rw-r--r--workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ExceptionsHandler.java76
-rw-r--r--workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/UnexpectedErrorResponse.java18
-rw-r--r--workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java54
3 files changed, 122 insertions, 26 deletions
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ExceptionsHandler.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ExceptionsHandler.java
index 5e6fc07d..f46d19be 100644
--- a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ExceptionsHandler.java
+++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/ExceptionsHandler.java
@@ -17,12 +17,15 @@
package org.onap.sdc.workflow.api;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
-import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
+import java.util.function.Function;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.commons.text.RandomStringGenerator;
import org.onap.sdc.workflow.api.types.ErrorResponse;
+import org.onap.sdc.workflow.api.types.UnexpectedErrorResponse;
import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException;
import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
@@ -30,6 +33,8 @@ import org.onap.sdc.workflow.services.exceptions.VersionCreationException;
import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException;
import org.onap.sdc.workflow.services.exceptions.VersionStatusModificationException;
+import org.openecomp.sdc.logging.api.Logger;
+import org.openecomp.sdc.logging.api.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -46,44 +51,63 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
@RestController
public class ExceptionsHandler extends ResponseEntityExceptionHandler {
- @ExceptionHandler(EntityNotFoundException.class)
- public final ResponseEntity<ErrorResponse> handleNotFoundException(Exception exception) {
- return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), NOT_FOUND);
- }
+ private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionsHandler.class);
+ private static final String LOG_MSG = "Exception was mapped to {} response";
+ private static final String UNEXPECTED_ERROR_MSG = "Something bad happened. Please contact support with code %s";
+ private static final RandomStringGenerator CODE_GENERATOR =
+ new RandomStringGenerator.Builder().withinRange('A', 'Z').build();
+ private static final int CODE_LENGTH = 8;
- @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
- VersionStateModificationException.class, VersionStatusModificationException.class,
- UniqueValueViolationException.class})
- public final ResponseEntity<ErrorResponse> handleUnprocessableEntityException(Exception exception) {
- return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), UNPROCESSABLE_ENTITY);
- }
+ private static final Function<Exception, UnexpectedErrorResponse> UNEXPECTED_EXCEPTION_MAPPER =
+ isDevInfoDisabled()
+ ? e -> new UnexpectedErrorResponse(getUnexpectedErrorMessage())
+ : e -> new UnexpectedErrorResponse(getUnexpectedErrorMessage(), ExceptionUtils.getStackTrace(e));
- @ExceptionHandler(VersionCreationException.class)
- public final ResponseEntity<ErrorResponse> handleVersioningErrorException(VersionCreationException exception) {
- return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), FORBIDDEN);
- }
-
- @ExceptionHandler(Exception.class)
- public final ResponseEntity<ErrorResponse> handleUnexpectedException(Exception exception) {
- return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), INTERNAL_SERVER_ERROR);
- }
-
- //For missing header exceptions
@Override
public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {
+ //For missing header exceptions
+ LOGGER.debug(LOG_MSG, BAD_REQUEST, ex);
return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), BAD_REQUEST);
}
@Override
protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException exception,
- final HttpHeaders headers,
- final HttpStatus status,
- final WebRequest request) {
-
+ final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
+ LOGGER.debug(LOG_MSG, BAD_REQUEST, exception);
String errorMsg = exception.getBindingResult().getFieldErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage).findFirst()
.orElse(exception.getMessage());
return new ResponseEntity<>(new ErrorResponse(errorMsg), BAD_REQUEST);
}
+
+ @ExceptionHandler(EntityNotFoundException.class)
+ public final ResponseEntity<ErrorResponse> handleNotFoundException(Exception exception) {
+ LOGGER.debug(LOG_MSG, NOT_FOUND, exception);
+ return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), NOT_FOUND);
+ }
+
+ @ExceptionHandler(
+ {InvalidArtifactException.class, VersionCreationException.class, VersionModificationException.class,
+ VersionStateModificationException.class, VersionStatusModificationException.class,
+ UniqueValueViolationException.class})
+ public final ResponseEntity<ErrorResponse> handleUnprocessableEntityException(Exception exception) {
+ LOGGER.debug(LOG_MSG, UNPROCESSABLE_ENTITY, exception);
+ return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), UNPROCESSABLE_ENTITY);
+ }
+
+ @ExceptionHandler(Exception.class)
+ public final ResponseEntity<UnexpectedErrorResponse> handleUnexpectedException(Exception exception) {
+ UnexpectedErrorResponse response = UNEXPECTED_EXCEPTION_MAPPER.apply(exception);
+ LOGGER.error(response.getMessage(), exception);
+ return new ResponseEntity<>(response, INTERNAL_SERVER_ERROR);
+ }
+
+ private static boolean isDevInfoDisabled() {
+ return Boolean.FALSE.toString().equalsIgnoreCase(System.getProperty("errors.includeDevInfo"));
+ }
+
+ private static String getUnexpectedErrorMessage() {
+ return String.format(UNEXPECTED_ERROR_MSG, CODE_GENERATOR.generate(CODE_LENGTH));
+ }
}
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/UnexpectedErrorResponse.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/UnexpectedErrorResponse.java
new file mode 100644
index 00000000..fbc5ab39
--- /dev/null
+++ b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/types/UnexpectedErrorResponse.java
@@ -0,0 +1,18 @@
+package org.onap.sdc.workflow.api.types;
+
+import lombok.Getter;
+
+@Getter
+public class UnexpectedErrorResponse extends ErrorResponse {
+
+ private String devInfo;
+
+ public UnexpectedErrorResponse(String message) {
+ super(message);
+ }
+
+ public UnexpectedErrorResponse(String message, String devInfo) {
+ super(message);
+ this.devInfo = devInfo;
+ }
+}
diff --git a/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java
new file mode 100644
index 00000000..e4008bb0
--- /dev/null
+++ b/workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ExceptionsHandlerTest.java
@@ -0,0 +1,54 @@
+package org.onap.sdc.workflow.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
+import static org.springframework.http.HttpStatus.NOT_FOUND;
+import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.onap.sdc.workflow.api.types.ErrorResponse;
+import org.onap.sdc.workflow.api.types.UnexpectedErrorResponse;
+import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
+import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+public class ExceptionsHandlerTest {
+
+ @InjectMocks
+ private ExceptionsHandler exceptionsHandler;
+
+ @Test
+ public void handleNotFoundException() {
+ EntityNotFoundException exception = new EntityNotFoundException("message");
+ ResponseEntity<ErrorResponse> response = exceptionsHandler.handleNotFoundException(exception);
+
+ assertEquals(NOT_FOUND, response.getStatusCode());
+ assertEquals(exception.getMessage(), response.getBody().getMessage());
+ }
+
+ @Test
+ public void handleUnprocessableEntityException() {
+ VersionModificationException exception = new VersionModificationException("1", "2");
+ ResponseEntity<ErrorResponse> response = exceptionsHandler.handleUnprocessableEntityException(exception);
+
+ assertEquals(UNPROCESSABLE_ENTITY, response.getStatusCode());
+ assertEquals(exception.getMessage(), response.getBody().getMessage());
+ }
+
+ @Test
+ public void handleUnexpectedException() {
+ Exception exception = new Exception("message");
+ ResponseEntity<UnexpectedErrorResponse> response = exceptionsHandler.handleUnexpectedException(exception);
+
+ assertEquals(INTERNAL_SERVER_ERROR, response.getStatusCode());
+ assertNotNull(response.getBody().getMessage());
+ assertFalse(response.getBody().getMessage().contains(exception.getMessage()));
+ assertNotNull(response.getBody().getDevInfo());
+ }
+} \ No newline at end of file