Migrate pap startup & controllers to spring boot
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / PapExceptionHandler.java
1 /*-
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
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.pap.main;
22
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;
35
36 @RestControllerAdvice
37 public class PapExceptionHandler {
38
39     private static final Logger logger = LoggerFactory.getLogger(PapExceptionHandler.class);
40
41     /**
42      * Handle PfModelException.
43      *
44      * @return ResponseEntity the response
45      */
46     @ExceptionHandler(PfModelException.class)
47     public ResponseEntity<ErrorResponse> pfModelExceptionHandler(PfModelException exp, WebRequest req) {
48         return handlePfModelException(exp, exp.getErrorResponse(), req);
49     }
50
51     /**
52      * Handle PfModelRuntimeException.
53      *
54      * @return ResponseEntity the response
55      */
56     @ExceptionHandler(PfModelRuntimeException.class)
57     public ResponseEntity<ErrorResponse> pfModelRuntimeExceptionHandler(PfModelRuntimeException exp, WebRequest req) {
58         return handlePfModelException(exp, exp.getErrorResponse(), req);
59     }
60
61     private ResponseEntity<ErrorResponse> handlePfModelException(Exception exp, ErrorResponse errorResponse,
62         WebRequest req) {
63         logger.warn(exp.getMessage(), exp);
64         String requestId = req.getHeader(PapRestControllerV1.REQUEST_ID_NAME);
65         return PapRestControllerV1.addLoggingHeaders(
66             PapRestControllerV1
67                 .addVersionControlHeaders(ResponseEntity.status(errorResponse.getResponseCode().getStatusCode())),
68             requestId != null ? UUID.fromString(requestId) : null).body(errorResponse);
69     }
70
71     /**
72      * Handle IllegalArgumentException.
73      *
74      * @return ResponseEntity the response
75      */
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);
84     }
85 }