2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.pap.main;
23 import java.util.UUID;
24 import org.onap.policy.models.base.PfModelException;
25 import org.onap.policy.models.base.PfModelRuntimeException;
26 import org.onap.policy.models.errors.concepts.ErrorResponse;
27 import org.onap.policy.pap.main.rest.PapRestControllerV1;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpStatus;
31 import org.springframework.http.ResponseEntity;
32 import org.springframework.web.bind.annotation.ExceptionHandler;
33 import org.springframework.web.bind.annotation.RestControllerAdvice;
34 import org.springframework.web.context.request.WebRequest;
37 public class PapExceptionHandler {
39 private static final Logger logger = LoggerFactory.getLogger(PapExceptionHandler.class);
42 * Handle PfModelException.
44 * @return ResponseEntity the response
46 @ExceptionHandler(PfModelException.class)
47 public ResponseEntity<ErrorResponse> pfModelExceptionHandler(PfModelException exp, WebRequest req) {
48 return handlePfModelException(exp, exp.getErrorResponse(), req);
52 * Handle PfModelRuntimeException.
54 * @return ResponseEntity the response
56 @ExceptionHandler(PfModelRuntimeException.class)
57 public ResponseEntity<ErrorResponse> pfModelRuntimeExceptionHandler(PfModelRuntimeException exp, WebRequest req) {
58 return handlePfModelException(exp, exp.getErrorResponse(), req);
61 private ResponseEntity<ErrorResponse> handlePfModelException(Exception exp, ErrorResponse errorResponse,
63 logger.warn(exp.getMessage(), exp);
64 String requestId = req.getHeader(PapRestControllerV1.REQUEST_ID_NAME);
65 return PapRestControllerV1.addLoggingHeaders(
67 .addVersionControlHeaders(ResponseEntity.status(errorResponse.getResponseCode().getStatusCode())),
68 requestId != null ? UUID.fromString(requestId) : null).body(errorResponse);
72 * Handle IllegalArgumentException.
74 * @return ResponseEntity the response
76 @ExceptionHandler(IllegalArgumentException.class)
77 public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException exp, WebRequest req) {
78 String errorMessage = exp.getClass().getName() + " " + exp.getMessage();
79 logger.warn(exp.getMessage(), exp);
80 String requestId = req.getHeader(PapRestControllerV1.REQUEST_ID_NAME);
81 return PapRestControllerV1.addLoggingHeaders(
82 PapRestControllerV1.addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
83 requestId != null ? UUID.fromString(requestId) : null).body(errorMessage);