c68e6d32cfffb10b0024fd8bae861b8d1f97a56e
[externalapi/nbi.git] / src / main / java / org / onap / nbi / exceptions / ApiExceptionHandler.java
1 /**
2  *     Copyright (c) 2018 Orange
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.nbi.exceptions;
18
19 import org.springframework.http.HttpStatus;
20 import org.springframework.http.ResponseEntity;
21 import org.springframework.web.bind.annotation.ControllerAdvice;
22 import org.springframework.web.bind.annotation.ExceptionHandler;
23 import org.springframework.web.bind.annotation.ResponseBody;
24 import org.springframework.web.client.RestClientException;
25
26 @ControllerAdvice
27 public class ApiExceptionHandler {
28
29     @ExceptionHandler(BackendFunctionalException.class)
30     @ResponseBody
31     public ResponseEntity<ApiError> backendExceptionHandler(final BackendFunctionalException exception) {
32         ApiError apiError =
33                 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
34         return new ResponseEntity<>(apiError, exception.getHttpStatus());
35     }
36
37     @ExceptionHandler(TechnicalException.class)
38     @ResponseBody
39     public ResponseEntity<ApiError> technicalExceptionHandler(final TechnicalException exception) {
40         ApiError apiError =
41                 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
42         return new ResponseEntity<>(apiError, exception.getHttpStatus());
43     }
44
45     @ExceptionHandler(RestClientException.class)
46     @ResponseBody
47     public ResponseEntity<ApiError> restClientExceptionHandler(final RestClientException exception) {
48         ApiError apiError = new ApiError("500", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
49                 "Unable to " + "reach ONAP services", "");
50         return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
51     }
52
53     @ExceptionHandler(ValidationException.class)
54     @ResponseBody
55     public ResponseEntity<ApiError> validationExceptionHandler(final ValidationException exception) {
56         ApiError apiError = new ApiError("400", HttpStatus.BAD_REQUEST.getReasonPhrase(), exception.getMessages(), "");
57         return new ResponseEntity<>(apiError, HttpStatus.BAD_REQUEST);
58     }
59 }