Exception stack trace is exposed
[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.onap.cps.rest.controller.AdminRestController;
24 import org.onap.cps.rest.controller.DataRestController;
25 import org.onap.cps.rest.controller.QueryRestController;
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.CpsPathException;
30 import org.onap.cps.spi.exceptions.DataInUseException;
31 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
32 import org.onap.cps.spi.exceptions.DataValidationException;
33 import org.onap.cps.spi.exceptions.ModelValidationException;
34 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.ExceptionHandler;
38 import org.springframework.web.bind.annotation.RestControllerAdvice;
39
40 @Slf4j
41 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class,
42     QueryRestController.class})
43 public class CpsRestExceptionHandler {
44
45     private static final String checkLogsForDetails  = "Check logs for details.";
46
47     private CpsRestExceptionHandler() {
48     }
49
50     /**
51      * Default exception handler.
52      *
53      * @param exception the exception to handle
54      * @return response with response code 500.
55      */
56     @ExceptionHandler
57     public static ResponseEntity<Object> handleInternalServerErrorExceptions(
58         final Exception exception) {
59         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
60     }
61
62     @ExceptionHandler({ModelValidationException.class, DataValidationException.class, CpsAdminException.class,
63         CpsPathException.class})
64     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
65         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception);
66     }
67
68     @ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
69     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
70         return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
71     }
72
73     @ExceptionHandler({DataInUseException.class})
74     public static ResponseEntity<Object> handleDataInUseException(final CpsException exception) {
75         return buildErrorResponse(HttpStatus.CONFLICT, exception);
76     }
77
78     @ExceptionHandler({CpsException.class})
79     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
80         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
81     }
82
83     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
84         if (exception.getCause() != null || !(exception instanceof CpsException)) {
85             log.error("Exception occurred", exception);
86         }
87         final ErrorMessage errorMessage = new ErrorMessage();
88         errorMessage.setStatus(status.toString());
89         errorMessage.setMessage(exception.getMessage());
90         errorMessage.setDetails(exception instanceof CpsException ? ((CpsException) exception).getDetails() :
91             checkLogsForDetails);
92         return new ResponseEntity<>(errorMessage, status);
93     }
94 }