Fetching data node by xpath - rest and service layers
[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  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.rest.exceptions;
21
22 import lombok.extern.slf4j.Slf4j;
23 import org.apache.commons.lang3.exception.ExceptionUtils;
24 import org.onap.cps.rest.controller.AdminRestController;
25 import org.onap.cps.rest.controller.DataRestController;
26 import org.onap.cps.rest.model.ErrorMessage;
27 import org.onap.cps.spi.exceptions.CpsAdminException;
28 import org.onap.cps.spi.exceptions.CpsException;
29 import org.onap.cps.spi.exceptions.DataInUseException;
30 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
31 import org.onap.cps.spi.exceptions.DataValidationException;
32 import org.onap.cps.spi.exceptions.ModelValidationException;
33 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.ExceptionHandler;
37 import org.springframework.web.bind.annotation.RestControllerAdvice;
38
39 @Slf4j
40 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class})
41 public class CpsRestExceptionHandler {
42
43     private CpsRestExceptionHandler() {
44     }
45
46     /**
47      * Default exception handler.
48      *
49      * @param exception the exception to handle
50      * @return response with response code 500.
51      */
52     @ExceptionHandler public static ResponseEntity<Object> handleInternalServerErrorExceptions(
53         final Exception exception) {
54         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
55     }
56
57     @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class})
58     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
59         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception));
60     }
61
62     @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
63     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
64         return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception));
65     }
66
67     @ExceptionHandler({DataInUseException.class})
68     public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
69         return buildErrorResponse(HttpStatus.CONFLICT, exception.getMessage(), extractDetails(exception));
70     }
71
72     @ExceptionHandler({CpsException.class})
73     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
74         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception));
75     }
76
77     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
78         return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception));
79     }
80
81     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message,
82         final String details) {
83         log.error("An error has occurred : {} Status: {} Details: {}", message, status, details);
84         final ErrorMessage errorMessage = new ErrorMessage();
85         errorMessage.setStatus(status.toString());
86         errorMessage.setMessage(message);
87         errorMessage.setDetails(details);
88         return new ResponseEntity<>(errorMessage, status);
89     }
90
91     private static String extractDetails(final CpsException exception) {
92         return exception.getCause() == null
93             ? exception.getDetails()
94             : ExceptionUtils.getStackTrace(exception.getCause());
95     }
96 }