bcc28d6d1dc419dce934119bc71fa87b7d1f94c6
[sdc/sdc-workflow-designer.git] /
1 package org.onap.sdc.workflow.api.exceptionshandlers;
2
3 import static org.springframework.http.HttpStatus.BAD_REQUEST;
4 import static org.springframework.http.HttpStatus.FORBIDDEN;
5 import static org.springframework.http.HttpStatus.NOT_FOUND;
6 import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
7
8 import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
9 import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException;
10 import org.onap.sdc.workflow.services.exceptions.InvalidPaginationParameterException;
11 import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
12 import org.onap.sdc.workflow.services.exceptions.VersionCreationException;
13 import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
14 import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException;
15 import org.springframework.http.HttpHeaders;
16 import org.springframework.http.HttpStatus;
17 import org.springframework.http.ResponseEntity;
18 import org.springframework.web.bind.ServletRequestBindingException;
19 import org.springframework.web.bind.annotation.ControllerAdvice;
20 import org.springframework.web.bind.annotation.ExceptionHandler;
21 import org.springframework.web.bind.annotation.RestController;
22 import org.springframework.web.context.request.WebRequest;
23 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
24
25 @ControllerAdvice
26 @RestController
27 public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
28
29     @ExceptionHandler(UniqueValueViolationException.class)
30     public final ResponseEntity<String> handleUniqueValueViolationException(
31             UniqueValueViolationException exception) {
32         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
33     }
34
35     @ExceptionHandler(EntityNotFoundException.class)
36     public final ResponseEntity<String> handleWorkflowNotFoundException(
37             Exception exception) {
38         return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
39     }
40
41     @ExceptionHandler({InvalidPaginationParameterException.class})
42     public final ResponseEntity<String> handlePaginationException(InvalidPaginationParameterException exception) {
43         return new ResponseEntity<>(exception.getMessage(), BAD_REQUEST);
44     }
45
46     //For missing header exceptions
47     @Override
48     public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
49                                                                        HttpHeaders headers, HttpStatus status,
50                                                                        WebRequest request) {
51         return new ResponseEntity<>(ex.getMessage(), BAD_REQUEST);
52     }
53
54
55     @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
56             VersionStateModificationException.class})
57     public final ResponseEntity<String> handleInvalidArtifactException(
58             Exception exception) {
59         return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
60     }
61
62
63     @ExceptionHandler(VersionCreationException.class)
64     public final ResponseEntity<String> handleVersioningErrorException(
65             VersionCreationException exception) {
66         return new ResponseEntity<>(exception.getMessage(), FORBIDDEN);
67     }
68 }