d91ba21777da76117bc88835ca387945d67c0112
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.clamp.common.acm.exception;
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 automation composition run time exception from which all automation composition run time
33  * exceptions are sub classes.
34  */
35 @Getter
36 @ToString
37 public class AutomationCompositionRuntimeException extends RuntimeException implements ErrorResponseInfo {
38
39     @Serial
40     private static final long serialVersionUID = -8507246953751956974L;
41
42     // The error response of the exception
43     private final ErrorResponse errorResponse = new ErrorResponse();
44
45     // The object on which the exception was thrown
46     private final transient Object object;
47
48     /**
49      * Instantiates a new automation composition runtime exception.
50      *
51      * @param statusCode the return code for the exception
52      * @param message the message on the exception
53      */
54     public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message) {
55         this(statusCode, message, null);
56     }
57
58     /**
59      * Instantiates a new automation composition runtime exception.
60      *
61      * @param statusCode the return code for the exception
62      * @param message the message on the exception
63      * @param object the object that the exception was thrown on
64      */
65     public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message,
66         final Object object) {
67         super(message);
68         this.object = object;
69         errorResponse.setResponseCode(statusCode);
70         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
71     }
72
73     /**
74      * Instantiates a new automation composition runtime exception.
75      *
76      * @param statusCode the return code for the exception
77      * @param message the message on the exception
78      * @param exception the exception that caused this automation composition exception
79      */
80     public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message,
81         final Exception exception) {
82         this(statusCode, message, exception, null);
83     }
84
85     /**
86      * Instantiates a new model runtime exception from an AutomationCompositionException instance.
87      *
88      * @param exception the exception that caused this automation composition exception
89      */
90     public AutomationCompositionRuntimeException(final AutomationCompositionException exception) {
91         super(exception.getMessage(), exception);
92         this.object = exception.getObject();
93         errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode());
94         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
95     }
96
97     /**
98      * Instantiates a new automation composition runtime exception.
99      *
100      * @param statusCode the return code for the exception
101      * @param message the message on the exception
102      * @param exception the exception that caused this automation composition exception
103      * @param object the object that the exception was thrown on
104      */
105     public AutomationCompositionRuntimeException(final Response.Status statusCode, final String message,
106         final Exception exception, final Object object) {
107         super(message, exception);
108         this.object = object;
109         errorResponse.setResponseCode(statusCode);
110         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
111     }
112 }