Merge "Expose Prometheus metrics for monitoring"
[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 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  *
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  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.rest.exceptions;
23
24 import javax.servlet.http.HttpServletRequest;
25 import lombok.extern.slf4j.Slf4j;
26 import org.onap.cps.rest.controller.AdminRestController;
27 import org.onap.cps.rest.controller.DataRestController;
28 import org.onap.cps.rest.controller.QueryRestController;
29 import org.onap.cps.rest.model.ErrorMessage;
30 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
31 import org.onap.cps.spi.exceptions.CpsAdminException;
32 import org.onap.cps.spi.exceptions.CpsException;
33 import org.onap.cps.spi.exceptions.CpsPathException;
34 import org.onap.cps.spi.exceptions.DataInUseException;
35 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
36 import org.onap.cps.spi.exceptions.DataValidationException;
37 import org.onap.cps.spi.exceptions.ModelValidationException;
38 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.ExceptionHandler;
43 import org.springframework.web.bind.annotation.RestControllerAdvice;
44
45 @Slf4j
46 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
47     QueryRestController.class})
48 public class CpsRestExceptionHandler {
49
50     private CpsRestExceptionHandler() {
51     }
52
53     /**
54      * Default exception handler.
55      *
56      * @param exception the exception to handle
57      * @return response with response code 500.
58      */
59     @ExceptionHandler
60     public static ResponseEntity<Object> handleInternalServerErrorExceptions(final Exception exception) {
61         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
62     }
63
64     @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
65         CpsPathException.class})
66     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
67         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
68     }
69
70     @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
71     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception,
72         final HttpServletRequest request) {
73         return buildErrorResponse(HttpMethod.GET.matches(request.getMethod())
74             ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST, exception);
75     }
76
77     @ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class})
78     public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
79         return buildErrorResponse(HttpStatus.CONFLICT, exception);
80     }
81
82     @ExceptionHandler({CpsException.class})
83     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
84         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
85     }
86
87     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
88         if (exception.getCause() != null || !(exception instanceof CpsException)) {
89             log.error("Exception occurred", exception);
90         }
91         final var errorMessage = new ErrorMessage();
92         errorMessage.setStatus(status.toString());
93         errorMessage.setMessage(exception.getMessage());
94         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
95             "Check logs for details.");
96         return new ResponseEntity<>(errorMessage, status);
97     }
98 }