2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.nbi.exceptions;
19 import com.fasterxml.jackson.core.JsonProcessingException;
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;
28 public class ApiExceptionHandler {
30 @ExceptionHandler(BackendFunctionalException.class)
32 public ResponseEntity<ApiError> backendExceptionHandler(final BackendFunctionalException exception) {
34 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
35 return new ResponseEntity<>(apiError, exception.getHttpStatus());
38 @ExceptionHandler(TechnicalException.class)
40 public ResponseEntity<ApiError> technicalExceptionHandler(final TechnicalException exception) {
42 new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
43 return new ResponseEntity<>(apiError, exception.getHttpStatus());
46 @ExceptionHandler(RestClientException.class)
48 public ResponseEntity<ApiError> restClientExceptionHandler(final RestClientException exception) {
49 ApiError apiError = new ApiError("500", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
50 "Unable to " + "reach ONAP services", "");
51 return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
54 @ExceptionHandler(ValidationException.class)
56 public ResponseEntity<ApiError> validationExceptionHandler(final ValidationException exception) {
57 ApiError apiError = new ApiError("400", HttpStatus.BAD_REQUEST.getReasonPhrase(), exception.getMessages(), "");
58 return new ResponseEntity<>(apiError, HttpStatus.BAD_REQUEST);
61 @ExceptionHandler(JsonProcessingException.class)
63 public ResponseEntity<ApiError> validationExceptionHandler(final JsonProcessingException exception) {
64 ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST.name(), HttpStatus.BAD_REQUEST.getReasonPhrase(), "Request data is invalid!", "");
65 return new ResponseEntity<>(apiError, HttpStatus.BAD_REQUEST);