Add basic model object concepts
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfModelException.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 /**
24  * This class is a base exception from which all model exceptions are sub classes.
25  */
26 public class PfModelException extends Exception {
27     private static final long serialVersionUID = -8507246953751956974L;
28
29     // The object on which the exception was thrown
30     private final transient Object object;
31
32     /**
33      * Instantiates a new model exception.
34      *
35      * @param message the message on the exception
36      */
37     public PfModelException(final String message) {
38         this(message, null);
39     }
40
41     /**
42      * Instantiates a new model exception.
43      *
44      * @param message the message on the exception
45      * @param object the object that the exception was thrown on
46      */
47     public PfModelException(final String message, final Object object) {
48         super(message);
49         this.object = object;
50     }
51
52     /**
53      * Instantiates a new model exception.
54      *
55      * @param message the message on the exception
56      * @param exception the exception that caused this exception
57      */
58     public PfModelException(final String message, final Exception exception) {
59         this(message, exception, null);
60     }
61
62     /**
63      * Instantiates a new exception.
64      *
65      * @param message the message on the exception
66      * @param exception the exception that caused this exception
67      * @param object the object that the exception was thrown on
68      */
69     public PfModelException(final String message, final Exception exception, final Object object) {
70         super(message, exception);
71         this.object = object;
72     }
73
74     /**
75      * Get the message from this exception and its causes.
76      *
77      * @return the cascaded messages from this exception and the exceptions that caused it
78      */
79     public String getCascadedMessage() {
80         return buildCascadedMessage(this);
81     }
82
83     /**
84      * Build a cascaded message from an exception and all its nested exceptions.
85      *
86      * @param throwable the top level exception
87      * @return cascaded message string
88      */
89     public static String buildCascadedMessage(Throwable throwable) {
90         final StringBuilder builder = new StringBuilder();
91         builder.append(throwable.getMessage());
92
93         for (Throwable t = throwable; t != null; t = t.getCause()) {
94             builder.append("\ncaused by: ");
95             builder.append(t.getMessage());
96         }
97
98         return builder.toString();
99     }
100
101     /**
102      * Get the object on which the exception was thrown.
103      *
104      * @return The object on which the exception was thrown
105      */
106     public Object getObject() {
107         return object;
108     }
109 }