Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / exception / ServiceExceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2023 Bell Canada. All rights reserved.
4  * Modifications Copyright (C) 2023 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.policy.api.main.exception;
23
24 import jakarta.ws.rs.core.Response;
25 import org.aspectj.lang.JoinPoint;
26 import org.aspectj.lang.annotation.AfterThrowing;
27 import org.aspectj.lang.annotation.Aspect;
28 import org.onap.policy.models.base.PfModelRuntimeException;
29 import org.onap.policy.models.errors.concepts.ErrorResponse;
30 import org.springframework.stereotype.Component;
31 import org.springframework.transaction.TransactionException;
32
33 @Aspect
34 @Component
35 public class ServiceExceptionHandler {
36
37     /**
38      * Handle any exceptions that are not already handled.
39      * For e.g., runtime exceptions that could happen during SQL query execution related to data integrity etc.
40      *
41      * @param joinPoint the point of execution
42      * @param exception the exception
43      */
44     @AfterThrowing(pointcut = "execution(* org.onap.policy.api.main.service.*.*(..))", throwing = "exception")
45     public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
46         if (exception instanceof PolicyApiRuntimeException || exception instanceof PfModelRuntimeException) {
47             throw exception;
48         } else {
49             final var errorResponse = new ErrorResponse();
50             errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
51             errorResponse.setErrorMessage(exception.getMessage());
52             throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);
53         }
54     }
55
56     /**
57      * Handle DB Transaction related exceptions.
58      * All service classes in org.onap.policy.api.main.service are transactional and autowiring these service classes
59      * can cause TransactionException.
60      * For e.g., JDBC connection failure occurs and failed to open transaction at service level
61      *
62      * @param joinPoint the point of execution
63      * @param exception the exception
64      */
65     @AfterThrowing(pointcut = "execution(* org.onap.policy.api.main..*.*(..))", throwing = "exception")
66     public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
67         final var errorResponse = new ErrorResponse();
68         errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
69         errorResponse.setErrorMessage(exception.getMessage());
70         throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);
71     }
72 }