Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfModelException.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
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.models.base;
22
23 import jakarta.ws.rs.core.Response;
24 import java.io.Serial;
25 import lombok.Getter;
26 import lombok.ToString;
27 import org.onap.policy.models.errors.concepts.ErrorResponse;
28 import org.onap.policy.models.errors.concepts.ErrorResponseInfo;
29 import org.onap.policy.models.errors.concepts.ErrorResponseUtils;
30
31 /**
32  * This class is a base exception from which all model exceptions are subclasses.
33  */
34 @Getter
35 @ToString
36 public class PfModelException extends Exception implements ErrorResponseInfo {
37     @Serial
38     private static final long serialVersionUID = -8507246953751956974L;
39
40     // The error response of the exception
41     private final ErrorResponse errorResponse = new ErrorResponse();
42
43     // The object on which the exception was thrown
44     private final transient Object object;
45
46     /**
47      * Instantiates a new model exception.
48      *
49      * @param statusCode the return code for the exception
50      * @param message    the message on the exception
51      */
52     public PfModelException(final Response.Status statusCode, final String message) {
53         this(statusCode, message, null);
54     }
55
56     /**
57      * Instantiates a new model exception.
58      *
59      * @param statusCode the return code for the exception
60      * @param message    the message on the exception
61      * @param object     the object that the exception was thrown on
62      */
63     public PfModelException(final Response.Status statusCode, final String message, final Object object) {
64         super(message);
65         errorResponse.setResponseCode(statusCode);
66         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
67         this.object = object;
68     }
69
70     /**
71      * Instantiates a new model exception.
72      *
73      * @param statusCode the return code for the exception
74      * @param message    the message on the exception
75      * @param exception  the exception that caused this exception
76      */
77     public PfModelException(final Response.Status statusCode, final String message, final Exception exception) {
78         this(statusCode, message, exception, null);
79     }
80
81     /**
82      * Instantiates a new exception.
83      *
84      * @param statusCode the return code for the exception
85      * @param message    the message on the exception
86      * @param exception  the exception that caused this exception
87      * @param object     the object that the exception was thrown on
88      */
89     public PfModelException(final Response.Status statusCode, final String message, final Exception exception,
90                             final Object object) {
91         super(message, exception);
92         errorResponse.setResponseCode(statusCode);
93         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
94         this.object = object;
95     }
96 }