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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
24 * This class is a base exception from which all model exceptions are sub classes.
26 public class PfModelException extends Exception {
27 private static final long serialVersionUID = -8507246953751956974L;
29 // The object on which the exception was thrown
30 private final transient Object object;
33 * Instantiates a new model exception.
35 * @param message the message on the exception
37 public PfModelException(final String message) {
42 * Instantiates a new model exception.
44 * @param message the message on the exception
45 * @param object the object that the exception was thrown on
47 public PfModelException(final String message, final Object object) {
53 * Instantiates a new model exception.
55 * @param message the message on the exception
56 * @param exception the exception that caused this exception
58 public PfModelException(final String message, final Exception exception) {
59 this(message, exception, null);
63 * Instantiates a new exception.
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
69 public PfModelException(final String message, final Exception exception, final Object object) {
70 super(message, exception);
75 * Get the message from this exception and its causes.
77 * @return the cascaded messages from this exception and the exceptions that caused it
79 public String getCascadedMessage() {
80 return buildCascadedMessage(this);
84 * Build a cascaded message from an exception and all its nested exceptions.
86 * @param throwable the top level exception
87 * @return cascaded message string
89 public static String buildCascadedMessage(Throwable throwable) {
90 final StringBuilder builder = new StringBuilder();
91 builder.append(throwable.getMessage());
93 for (Throwable t = throwable; t != null; t = t.getCause()) {
94 builder.append("\ncaused by: ");
95 builder.append(t.getMessage());
98 return builder.toString();
102 * Get the object on which the exception was thrown.
104 * @return The object on which the exception was thrown
106 public Object getObject() {