2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.exception;
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.http.ResponseEntity;
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 ResponseEntity<Object> 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..*.*(..))"
66 + " && !execution(* org.onap.policy.api.main.rest.provider.statistics.*.*(..))", throwing = "exception")
67 public ResponseEntity<Object> handleTransactionException(JoinPoint joinPoint, TransactionException exception) {
68 final var errorResponse = new ErrorResponse();
69 errorResponse.setResponseCode(Response.Status.INTERNAL_SERVER_ERROR);
70 errorResponse.setErrorMessage(exception.getMessage());
71 throw new PolicyApiRuntimeException(exception.getMessage(), exception.getCause(), errorResponse, null);