2 * ========================LICENSE_START=================================
3 * Copyright (C) 2020 Nordix Foundation. All rights reserved.
4 * ======================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ========================LICENSE_END===================================
19 package org.oransc.rappcatalogue.api;
21 import static org.springframework.http.HttpStatus.BAD_REQUEST;
22 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
23 import static org.springframework.http.HttpStatus.NOT_FOUND;
25 import org.oransc.rappcatalogue.exception.HeaderException;
26 import org.oransc.rappcatalogue.exception.InvalidServiceException;
27 import org.oransc.rappcatalogue.exception.ServiceNotFoundException;
28 import org.oransc.rappcatalogue.model.ErrorInformation;
29 import org.springframework.http.HttpStatus;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.ControllerAdvice;
32 import org.springframework.web.bind.annotation.ExceptionHandler;
33 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
36 public class GeneralRappCatalogueControllerAdvisor extends ResponseEntityExceptionHandler {
37 @ExceptionHandler(InvalidServiceException.class)
38 public ResponseEntity<Object> handleInvalidServiceException(InvalidServiceException ex) {
40 return new ResponseEntity<>(getErrorInformation(ex, BAD_REQUEST), BAD_REQUEST);
43 @ExceptionHandler(ServiceNotFoundException.class)
44 public ResponseEntity<Object> handleServiceNotFoundException(ServiceNotFoundException ex) {
46 return new ResponseEntity<>(getErrorInformation(ex, NOT_FOUND), NOT_FOUND);
49 @ExceptionHandler(HeaderException.class)
50 public ResponseEntity<Object> handleHeaderException(HeaderException ex) {
52 return new ResponseEntity<>(getErrorInformation(ex, INTERNAL_SERVER_ERROR), INTERNAL_SERVER_ERROR);
55 private ErrorInformation getErrorInformation(Exception cause, HttpStatus status) {
56 ErrorInformation errorInfo = new ErrorInformation();
57 errorInfo.setDetail(cause.getMessage());
58 errorInfo.setStatus(status.value());