b110a43629dcd33f23621ae6985bebd44ba77c85
[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.controlloop.common.exception;
22
23 import javax.ws.rs.core.Response;
24 import lombok.Getter;
25 import lombok.ToString;
26 import org.onap.policy.models.errors.concepts.ErrorResponse;
27 import org.onap.policy.models.errors.concepts.ErrorResponseInfo;
28 import org.onap.policy.models.errors.concepts.ErrorResponseUtils;
29
30 /**
31  * This class is a base control loop run time exception from which all control loop run time exceptions are sub classes.
32  */
33 @Getter
34 @ToString
35 public class ControlLoopRuntimeException extends RuntimeException implements ErrorResponseInfo {
36     private static final long serialVersionUID = -8507246953751956974L;
37
38     // The error response of the exception
39     private final ErrorResponse errorResponse = new ErrorResponse();
40
41     // The object on which the exception was thrown
42     private final transient Object object;
43
44     /**
45      * Instantiates a new control loop runtime exception.
46      *
47      * @param statusCode the return code for the exception
48      * @param message the message on the exception
49      */
50     public ControlLoopRuntimeException(final Response.Status statusCode, final String message) {
51         this(statusCode, message, null);
52     }
53
54     /**
55      * Instantiates a new control loop runtime exception.
56      *
57      * @param statusCode the return code for the exception
58      * @param message the message on the exception
59      * @param object the object that the exception was thrown on
60      */
61     public ControlLoopRuntimeException(final Response.Status statusCode, final String message, final Object object) {
62         super(message);
63         this.object = object;
64         errorResponse.setResponseCode(statusCode);
65         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
66     }
67
68     /**
69      * Instantiates a new control loop runtime exception.
70      *
71      * @param statusCode the return code for the exception
72      * @param message the message on the exception
73      * @param exception the exception that caused this control loop exception
74      */
75     public ControlLoopRuntimeException(final Response.Status statusCode, final String message,
76             final Exception exception) {
77         this(statusCode, message, exception, null);
78     }
79
80     /**
81      * Instantiates a new model runtime exception from a ControlLoopException instance.
82      *
83      * @param exception the exception that caused this control loop exception
84      */
85     public ControlLoopRuntimeException(final ControlLoopException exception) {
86         super(exception.getMessage(), exception);
87         this.object = exception.getObject();
88         errorResponse.setResponseCode(exception.getErrorResponse().getResponseCode());
89         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
90     }
91
92     /**
93      * Instantiates a new control loop runtime exception.
94      *
95      * @param statusCode the return code for the exception
96      * @param message the message on the exception
97      * @param exception the exception that caused this control loop exception
98      * @param object the object that the exception was thrown on
99      */
100     public ControlLoopRuntimeException(final Response.Status statusCode, final String message,
101             final Exception exception, final Object object) {
102         super(message, exception);
103         this.object = object;
104         errorResponse.setResponseCode(statusCode);
105         ErrorResponseUtils.getExceptionMessages(errorResponse, this);
106     }
107 }