Add serviceCatalog rest services
[externalapi/nbi.git] / src / main / java / org / onap / nbi / exceptions / ApiExceptionHandler.java
1 package org.onap.nbi.exceptions;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.http.HttpStatus;
6 import org.springframework.http.ResponseEntity;
7 import org.springframework.web.bind.annotation.ControllerAdvice;
8 import org.springframework.web.bind.annotation.ExceptionHandler;
9 import org.springframework.web.bind.annotation.ResponseBody;
10 import org.springframework.web.client.RestClientException;
11
12 @ControllerAdvice
13 public class ApiExceptionHandler {
14
15     private static final Logger LOGGER = LoggerFactory.getLogger(ApiExceptionHandler.class);
16
17
18     @ExceptionHandler(BackendFunctionalException.class)
19     @ResponseBody
20     public ResponseEntity<ApiError> backendExceptionHandler(final BackendFunctionalException exception) {
21         ApiError apiError = new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
22         return new ResponseEntity<ApiError>(apiError, exception.getHttpStatus());
23     }
24
25     @ExceptionHandler(TechnicalException.class)
26     @ResponseBody
27     public ResponseEntity<ApiError> technicalExceptionHandler(final TechnicalException exception) {
28         ApiError apiError = new ApiError(String.valueOf(exception.getHttpStatus().value()), exception.getMessage(), "", "");
29         return new ResponseEntity<ApiError>(apiError, exception.getHttpStatus());
30     }
31
32     @ExceptionHandler(RestClientException.class)
33     @ResponseBody
34     public ResponseEntity<ApiError> RestClientExceptionHandler(final RestClientException exception) {
35         ApiError apiError = new ApiError("500", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "Unable to " +
36                 "reach ONAP services", "");
37         return new ResponseEntity<ApiError>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
38     }
39
40     @ExceptionHandler(ValidationException.class)
41     @ResponseBody
42     public ResponseEntity<ApiError> ValidationExceptionHandler(final ValidationException exception) {
43         ApiError apiError = new ApiError("400", HttpStatus.BAD_REQUEST.getReasonPhrase(), exception.getMessages(), "");
44         return new ResponseEntity<ApiError>(apiError, HttpStatus.INTERNAL_SERVER_ERROR);
45     }
46 }