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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.api.main.exception;
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;
35 public class ServiceExceptionHandler {
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.
41 * @param joinPoint the point of execution
42 * @param exception the exception
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) {
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);
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
62 * @param joinPoint the point of execution
63 * @param exception the exception
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);