Add interface to get info from exceptions
[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 import javax.ws.rs.core.Response;
24
25 import lombok.Getter;
26 import lombok.ToString;
27
28 import org.apache.commons.lang3.exception.ExceptionUtils;
29
30 /**
31  * This class is a base exception from which all model exceptions are sub classes.
32  */
33 @Getter
34 @ToString
35 public class PfModelException extends Exception implements PfModelExceptionInfo {
36     private static final long serialVersionUID = -8507246953751956974L;
37
38     // The status code on the exception
39     private final Response.Status statusCode;
40
41     // The object on which the exception was thrown
42     private final transient Object object;
43
44     /**
45      * Instantiates a new model exception.
46      *
47      * @param statusCode the return code for the exception
48      * @param message the message on the exception
49      */
50     public PfModelException(final Response.Status statusCode, final String message) {
51         this(statusCode, message, null);
52     }
53
54     /**
55      * Instantiates a new model 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 PfModelException(final Response.Status statusCode, final String message, final Object object) {
62         super(message);
63         this.statusCode = statusCode;
64         this.object = object;
65     }
66
67     /**
68      * Instantiates a new model exception.
69      *
70      * @param statusCode the return code for the exception
71      * @param message the message on the exception
72      * @param exception the exception that caused this exception
73      */
74     public PfModelException(final Response.Status statusCode, final String message, final Exception exception) {
75         this(statusCode, message, exception, null);
76     }
77
78     /**
79      * Instantiates a new exception.
80      *
81      * @param statusCode the return code for the exception
82      * @param message the message on the exception
83      * @param exception the exception that caused this exception
84      * @param object the object that the exception was thrown on
85      */
86     public PfModelException(final Response.Status statusCode, final String message, final Exception exception,
87             final Object object) {
88         super(message, exception);
89         this.statusCode = statusCode;
90         this.object = object;
91     }
92
93     /**
94      * Get the message from this exception and its causes.
95      *
96      * @return the cascaded messages from this exception and the exceptions that caused it
97      */
98     @Override
99     public String getCascadedMessage() {
100         return buildCascadedMessage(this);
101     }
102
103     /**
104      * Build a cascaded message from an exception and all its nested exceptions.
105      *
106      * @param throwable the top level exception
107      * @return cascaded message string
108      */
109     public static String buildCascadedMessage(Throwable throwable) {
110         final StringBuilder builder = new StringBuilder();
111         builder.append(throwable.getMessage());
112
113         for (Throwable t = throwable; t != null; t = t.getCause()) {
114             builder.append("\ncaused by: ");
115             builder.append(t.getMessage());
116         }
117
118         return builder.toString();
119     }
120
121     /**
122      * Get the stack trace of the exception as a string.
123      *
124      * @return the stack trace of this message as a string
125      */
126     @Override
127     public String getStackTraceAsString() {
128         return ExceptionUtils.getStackTrace(this);
129     }
130 }