0c6edd5b4e676b5af4ae4ecc4d3a48857f26a407
[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  *  Modifications Copyright (C) 2021-2022 Nordix Foundation
5  *  Modifications Copyright (C) 2022 TechMahindra Ltd.
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  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.rest.exceptions;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.validation.ValidationException;
27 import lombok.AccessLevel;
28 import lombok.NoArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.rest.controller.AdminRestController;
31 import org.onap.cps.rest.controller.DataRestController;
32 import org.onap.cps.rest.controller.QueryRestController;
33 import org.onap.cps.rest.model.ErrorMessage;
34 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
35 import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch;
36 import org.onap.cps.spi.exceptions.CpsAdminException;
37 import org.onap.cps.spi.exceptions.CpsException;
38 import org.onap.cps.spi.exceptions.CpsPathException;
39 import org.onap.cps.spi.exceptions.DataInUseException;
40 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
41 import org.onap.cps.spi.exceptions.DataValidationException;
42 import org.onap.cps.spi.exceptions.ModelValidationException;
43 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
44 import org.springframework.http.HttpMethod;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.http.converter.HttpMessageNotReadableException;
48 import org.springframework.web.bind.annotation.ExceptionHandler;
49 import org.springframework.web.bind.annotation.RestControllerAdvice;
50
51 @Slf4j
52 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
53     QueryRestController.class})
54 @NoArgsConstructor(access = AccessLevel.PACKAGE)
55 public class CpsRestExceptionHandler {
56
57     /**
58      * Default exception handler.
59      *
60      * @param exception the exception to handle
61      * @return response with response code 500.
62      */
63     @ExceptionHandler
64     public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
65         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
66     }
67
68     @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
69         CpsPathException.class, ValidationException.class, HttpMessageNotReadableException.class})
70     public static ResponseEntity<Object> handleBadRequestExceptions(final Exception exception) {
71         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
72     }
73
74     @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
75     public static ResponseEntity<Object> handleNotFoundExceptions(final Exception exception,
76         final HttpServletRequest request) {
77         return buildErrorResponse(HttpMethod.GET.matches(request.getMethod())
78             ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST, exception);
79     }
80
81     @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class, AlreadyDefinedExceptionBatch.class})
82     public static ResponseEntity<Object> handleDataInUseException(final Exception exception) {
83         return buildErrorResponse(HttpStatus.CONFLICT, exception);
84     }
85
86     @ExceptionHandler({CpsException.class})
87     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final Exception exception) {
88         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
89     }
90
91     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
92         if (exception.getCause() != null || !(exception instanceof CpsException)) {
93             log.error("Exception occurred", exception);
94         }
95         final var errorMessage = new ErrorMessage();
96         errorMessage.setStatus(status.toString());
97         errorMessage.setMessage(exception.getMessage());
98         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
99             "Check logs for details.");
100         return new ResponseEntity<>(errorMessage, status);
101     }
102 }