Response code fix (Bad Request instead of Not Found) when modifying non-existent...
[cps.git] / cps-rest / src / main / java / org / onap / cps / rest / exceptions / CpsRestExceptionHandler.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Pantheon.tech
4  *  Copyright (C) 2021 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        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  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.rest.exceptions;
22
23 import javax.servlet.http.HttpServletRequest;
24 import lombok.extern.slf4j.Slf4j;
25 import org.onap.cps.rest.controller.AdminRestController;
26 import org.onap.cps.rest.controller.DataRestController;
27 import org.onap.cps.rest.controller.QueryRestController;
28 import org.onap.cps.rest.model.ErrorMessage;
29 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
30 import org.onap.cps.spi.exceptions.CpsAdminException;
31 import org.onap.cps.spi.exceptions.CpsException;
32 import org.onap.cps.spi.exceptions.CpsPathException;
33 import org.onap.cps.spi.exceptions.DataInUseException;
34 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
35 import org.onap.cps.spi.exceptions.DataValidationException;
36 import org.onap.cps.spi.exceptions.ModelValidationException;
37 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.ExceptionHandler;
42 import org.springframework.web.bind.annotation.RestControllerAdvice;
43
44 @Slf4j
45 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
46     QueryRestController.class})
47 public class CpsRestExceptionHandler {
48
49     private CpsRestExceptionHandler() {
50     }
51
52     /**
53      * Default exception handler.
54      *
55      * @param exception the exception to handle
56      * @return response with response code 500.
57      */
58     @ExceptionHandler
59     public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
60         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
61     }
62
63     @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
64         CpsPathException.class})
65     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
66         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
67     }
68
69     @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
70     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception,
71         final HttpServletRequest request) {
72         return buildErrorResponse(HttpMethod.GET.matches(request.getMethod())
73             ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST, exception);
74     }
75
76     @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class})
77     public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
78         return buildErrorResponse(HttpStatus.CONFLICT, exception);
79     }
80
81     @ExceptionHandler({CpsException.class})
82     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
83         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
84     }
85
86     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
87         if (exception.getCause() != null || !(exception instanceof CpsException)) {
88             log.error("Exception occurred", exception);
89         }
90         final var errorMessage = new ErrorMessage();
91         errorMessage.setStatus(status.toString());
92         errorMessage.setMessage(exception.getMessage());
93         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
94             "Check logs for details.");
95         return new ResponseEntity<>(errorMessage, status);
96     }
97 }