Remove 'All rights reserved.' on apache 2 license
[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 package org.onap.nbi.exceptions;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 import org.springframework.http.HttpStatus;
21 import org.springframework.http.ResponseEntity;
22 import org.springframework.web.bind.annotation.ControllerAdvice;
23 import org.springframework.web.bind.annotation.ExceptionHandler;
24 import org.springframework.web.bind.annotation.ResponseBody;
25 import org.springframework.web.client.RestClientException;
26
27 @ControllerAdvice
28 public class ApiExceptionHandler {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(ApiExceptionHandler.class);
31
32
33     @ExceptionHandler(BackendFunctionalException.class)
34     @ResponseBody
35     public ResponseEntity<ApiError> backendExceptionHandler(final BackendFunctionalException exception) {
36         ApiError apiError =
37                 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
38         return new ResponseEntity<ApiError>(apiError, exception.getHttpStatus());
39     }
40
41     @ExceptionHandler(TechnicalException.class)
42     @ResponseBody
43     public ResponseEntity<ApiError> technicalExceptionHandler(final TechnicalException exception) {
44         ApiError apiError =
45                 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
46         return new ResponseEntity<ApiError>(apiError, exception.getHttpStatus());
47     }
48
49     @ExceptionHandler(RestClientException.class)
50     @ResponseBody
51     public ResponseEntity<ApiError> RestClientExceptionHandler(final RestClientException exception) {
52         ApiError apiError = new ApiError("500", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
53                 "Unable to " + "reach ONAP services", "");
54         return new ResponseEntity<ApiError>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
55     }
56
57     @ExceptionHandler(ValidationException.class)
58     @ResponseBody
59     public ResponseEntity<ApiError> ValidationExceptionHandler(final ValidationException exception) {
60         ApiError apiError = new ApiError("400", HttpStatus.BAD_REQUEST.getReasonPhrase(), exception.getMessages(), "");
61         return new ResponseEntity<ApiError>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
62     }
63 }