Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-app / src / main / java / org / o / ran / oam / nf / oam / adopter / app / controller / RestExceptionHandler.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.app.controller;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.AlreadyPresentException;
25 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.exceptions.NotFoundException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.validation.FieldError;
31 import org.springframework.web.bind.MethodArgumentNotValidException;
32 import org.springframework.web.bind.annotation.ExceptionHandler;
33 import org.springframework.web.bind.annotation.ResponseStatus;
34 import org.springframework.web.bind.annotation.RestControllerAdvice;
35
36 @RestControllerAdvice
37 public class RestExceptionHandler {
38     private static final Logger LOG = LoggerFactory.getLogger(RestExceptionHandler.class);
39
40     /**
41      * Handle Already Present Exceptions.
42      */
43     @ExceptionHandler({AlreadyPresentException.class})
44     public static ResponseEntity<Object> handleBadRequestExceptions(final AlreadyPresentException exception) {
45         LOG.error("Request failed", exception);
46         return ResponseEntity
47                 .badRequest()
48                 .body(exception.getMessage());
49     }
50
51     /**
52      * Handle Not Found Exceptions.
53      */
54     @ResponseStatus(HttpStatus.NOT_FOUND)
55     @ExceptionHandler({NotFoundException.class})
56     public static String handleNotFoundExceptions(final NotFoundException exception) {
57         LOG.error("Request failed", exception);
58         return exception.getMessage();
59     }
60
61     /**
62      * Handle MethodArgument Not Valid Exceptions.
63      */
64     @ResponseStatus(HttpStatus.BAD_REQUEST)
65     @ExceptionHandler(MethodArgumentNotValidException.class)
66     public Map<String, String> handleValidationExceptions(final MethodArgumentNotValidException ex) {
67         final Map<String, String> errors = new HashMap<>();
68         ex.getBindingResult().getAllErrors().forEach(error -> {
69             final String fieldName = ((FieldError) error).getField();
70             final String errorMessage = error.getDefaultMessage();
71             errors.put(fieldName, errorMessage);
72         });
73         return errors;
74     }
75 }