93240df9b0e883730b3ead9433d7dc7088ede4a9
[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  * ================================================================================
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  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.api.main.exception;
22
23 import javax.ws.rs.core.Response;
24 import org.aspectj.lang.JoinPoint;
25 import org.aspectj.lang.annotation.AfterThrowing;
26 import org.aspectj.lang.annotation.Aspect;
27 import org.onap.policy.models.base.PfModelRuntimeException;
28 import org.onap.policy.models.errors.concepts.ErrorResponse;
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.api.main.service.*.*(..))", throwing = "exception")
44     public void handleServiceException(JoinPoint joinPoint, RuntimeException exception) {
45         if (exception instanceof PolicyApiRuntimeException || exception instanceof PfModelRuntimeException) {
46             throw exception;
47         } else {
48             final var errorResponse = new ErrorResponse();
49             errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
50             errorResponse.setErrorMessage(exception.getMessage());
51             throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);
52         }
53     }
54
55     /**
56      * Handle DB Transaction related exceptions.
57      * All service classes in org.onap.policy.api.main.service are transactional and autowiring these service classes
58      * can cause TransactionException.
59      * For e.g., JDBC connection failure occurs and failed to open transaction at service level
60      *
61      * @param joinPoint the point of execution
62      * @param exception the exception
63      */
64     @AfterThrowing(pointcut = "execution(* org.onap.policy.api.main..*.*(..))", throwing = "exception")
65     public void handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
66         final var errorResponse = new ErrorResponse();
67         errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
68         errorResponse.setErrorMessage(exception.getMessage());
69         throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);
70     }
71 }