5e6fc07de4f920cbd4769509a190fd13c204dbc9
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / api / ExceptionsHandler.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.sdc.workflow.api;
18
19 import static org.springframework.http.HttpStatus.BAD_REQUEST;
20 import static org.springframework.http.HttpStatus.FORBIDDEN;
21 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
22 import static org.springframework.http.HttpStatus.NOT_FOUND;
23 import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
24
25 import org.onap.sdc.workflow.api.types.ErrorResponse;
26 import org.onap.sdc.workflow.services.exceptions.EntityNotFoundException;
27 import org.onap.sdc.workflow.services.exceptions.InvalidArtifactException;
28 import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
29 import org.onap.sdc.workflow.services.exceptions.VersionCreationException;
30 import org.onap.sdc.workflow.services.exceptions.VersionModificationException;
31 import org.onap.sdc.workflow.services.exceptions.VersionStateModificationException;
32 import org.onap.sdc.workflow.services.exceptions.VersionStatusModificationException;
33 import org.springframework.context.support.DefaultMessageSourceResolvable;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.MethodArgumentNotValidException;
38 import org.springframework.web.bind.ServletRequestBindingException;
39 import org.springframework.web.bind.annotation.ControllerAdvice;
40 import org.springframework.web.bind.annotation.ExceptionHandler;
41 import org.springframework.web.bind.annotation.RestController;
42 import org.springframework.web.context.request.WebRequest;
43 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
44
45 @ControllerAdvice
46 @RestController
47 public class ExceptionsHandler extends ResponseEntityExceptionHandler {
48
49     @ExceptionHandler(EntityNotFoundException.class)
50     public final ResponseEntity<ErrorResponse> handleNotFoundException(Exception exception) {
51         return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), NOT_FOUND);
52     }
53
54     @ExceptionHandler({InvalidArtifactException.class, VersionModificationException.class,
55             VersionStateModificationException.class, VersionStatusModificationException.class,
56             UniqueValueViolationException.class})
57     public final ResponseEntity<ErrorResponse> handleUnprocessableEntityException(Exception exception) {
58         return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), UNPROCESSABLE_ENTITY);
59     }
60
61     @ExceptionHandler(VersionCreationException.class)
62     public final ResponseEntity<ErrorResponse> handleVersioningErrorException(VersionCreationException exception) {
63         return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), FORBIDDEN);
64     }
65
66     @ExceptionHandler(Exception.class)
67     public final ResponseEntity<ErrorResponse> handleUnexpectedException(Exception exception) {
68         return new ResponseEntity<>(new ErrorResponse(exception.getMessage()), INTERNAL_SERVER_ERROR);
69     }
70
71     //For missing header exceptions
72     @Override
73     public ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
74             HttpHeaders headers, HttpStatus status, WebRequest request) {
75         return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), BAD_REQUEST);
76     }
77
78     @Override
79     protected final ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException exception,
80             final HttpHeaders headers,
81             final HttpStatus status,
82             final WebRequest request) {
83
84         String errorMsg = exception.getBindingResult().getFieldErrors().stream()
85                                   .map(DefaultMessageSourceResolvable::getDefaultMessage).findFirst()
86                                   .orElse(exception.getMessage());
87         return new ResponseEntity<>(new ErrorResponse(errorMsg), BAD_REQUEST);
88     }
89 }