Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfModelRuntimeException.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021, 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 model run time exception from which all model run time exceptions are subclasses.
33  */
34 @Getter
35 @ToString
36 public class PfModelRuntimeException extends RuntimeException 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 runtime exception.
48      *
49      * @param statusCode the return code for the exception
50      * @param message    the message on the exception
51      */
52     public PfModelRuntimeException(final Response.Status statusCode, final String message) {
53         this(statusCode, message, null);
54     }
55
56     /**
57      * Instantiates a new model runtime 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 PfModelRuntimeException(final Response.Status statusCode, final String message, final Object object) {
64         super(message);
65         this.object = object;
66         errorResponse.setResponseCode(statusCode);
67         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
68     }
69
70     /**
71      * Instantiates a new model runtime 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 model exception
76      */
77     public PfModelRuntimeException(final Response.Status statusCode, final String message, final Exception exception) {
78         this(statusCode, message, exception, null);
79     }
80
81     /**
82      * Instantiates a new model runtime exception from a PfModelException instance.
83      *
84      * @param exception the exception that caused this model exception
85      */
86     public PfModelRuntimeException(final PfModelException exception) {
87         super(exception.getMessage(), exception);
88         this.object = exception.getObject();
89         errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode());
90         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
91     }
92
93     /**
94      * Instantiates a new model runtime exception.
95      *
96      * @param statusCode the return code for the exception
97      * @param message    the message on the exception
98      * @param exception  the exception that caused this model exception
99      * @param object     the object that the exception was thrown on
100      */
101     public PfModelRuntimeException(final Response.Status statusCode, final String message, final Exception exception,
102                                    final Object object) {
103         super(message, exception);
104         this.object = object;
105         errorResponse.setResponseCode(statusCode);
106         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
107     }
108 }