Split the CpsRestController.java
[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 org.apache.commons.lang3.exception.ExceptionUtils;
23 import org.onap.cps.rest.controller.AdminRestController;
24 import org.onap.cps.rest.controller.DataRestController;
25 import org.onap.cps.rest.model.ErrorMessage;
26 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
27 import org.onap.cps.spi.exceptions.CpsAdminException;
28 import org.onap.cps.spi.exceptions.CpsException;
29 import org.onap.cps.spi.exceptions.DataValidationException;
30 import org.onap.cps.spi.exceptions.ModelValidationException;
31 import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
32 import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.web.bind.annotation.ExceptionHandler;
36 import org.springframework.web.bind.annotation.RestControllerAdvice;
37
38 @RestControllerAdvice(assignableTypes = {AdminRestController.class, DataRestController.class})
39 public class CpsRestExceptionHandler {
40
41     private CpsRestExceptionHandler() {
42     }
43
44     /**
45      * Default exception handler.
46      *
47      * @param exception the exception to handle
48      * @return response with response code 500.
49      */
50     @ExceptionHandler public static ResponseEntity<Object> handleInternalServerErrorExceptions(
51         final Exception exception) {
52         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception);
53     }
54
55     @ExceptionHandler({ModelValidationException.class, DataValidationException.class,
56         SchemaSetAlreadyDefinedException.class, AnchorAlreadyDefinedException.class, CpsAdminException.class})
57     public static ResponseEntity<Object> handleBadRequestExceptions(final CpsException exception) {
58         return buildErrorResponse(HttpStatus.BAD_REQUEST, exception.getMessage(), extractDetails(exception));
59     }
60
61     @ExceptionHandler({NotFoundInDataspaceException.class})
62     public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
63         return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception));
64     }
65
66     @ExceptionHandler({CpsException.class})
67     public static ResponseEntity<Object> handleAnyOtherCpsExceptions(final CpsException exception) {
68         return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception));
69     }
70
71     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final Exception exception) {
72         return buildErrorResponse(status, exception.getMessage(), ExceptionUtils.getStackTrace(exception));
73     }
74
75     private static ResponseEntity<Object> buildErrorResponse(final HttpStatus status, final String message,
76         final String details) {
77         final ErrorMessage errorMessage = new ErrorMessage();
78         errorMessage.setStatus(status.toString());
79         errorMessage.setMessage(message);
80         errorMessage.setDetails(details);
81         return new ResponseEntity<>(errorMessage, status);
82     }
83
84     private static String extractDetails(final CpsException exception) {
85         return exception.getCause() == null
86             ? exception.getDetails()
87             : ExceptionUtils.getStackTrace(exception.getCause());
88     }
89 }