Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / exception / ServiceExceptionHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.pap.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.springframework.stereotype.Component;
30 import org.springframework.transaction.TransactionException;
31
32 @Aspect
33 @Component
34 public class ServiceExceptionHandler {
35
36     /**
37      * Handle any exceptions that are not already handled.
38      * For e.g., runtime exceptions that could happen during SQL query execution related to data integrity etc.
39      *
40      * @param joinPoint the point of execution
41      * @param exception the exception
42      */
43     @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.service.*.*(..))", throwing = "exception")
44     public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
45         if (exception instanceof PfModelRuntimeException) {
46             throw (PfModelRuntimeException) exception;
47         } else {
48             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
49         }
50     }
51
52     /**
53      * Handle DB Transaction related exceptions.
54      * All service classes in org.onap.policy.pap.main.service are transactional now,
55      * Autowiring these service classes can cause TransactionException.
56      * For e.g., JDBC connection failure occurs and failed to open transaction at service level
57      *
58      * @param joinPoint the point of execution
59      * @param exception the exception
60      */
61     @AfterThrowing(pointcut = "execution(* org.onap.policy.pap.main.*.*.*(..))", throwing = "exception")
62     public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
63         throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, exception.getMessage(), exception);
64     }
65 }