f2253a30e8dbd17401d113090d4d180bcb7c6863
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / exceptions / AAIException.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.exceptions;
21
22 import java.util.Collection;
23 import java.util.LinkedList;
24
25 import org.onap.aai.logging.ErrorLogHelper;
26 import org.onap.aai.logging.ErrorObject;
27 import org.onap.aai.logging.ErrorObjectNotFoundException;
28
29 public class AAIException extends Exception {
30
31         public static final String DEFAULT_EXCEPTION_CODE = "AAI_4000";
32         private static final long serialVersionUID = 1L;
33
34         private final String code;
35         private final ErrorObject errorObject;
36         private final Collection<String> templateVars;
37
38         /**
39          * Instantiates a new AAI exception.
40          */
41         public AAIException() {
42                 super();
43                 this.code = DEFAULT_EXCEPTION_CODE;
44                 this.templateVars = new LinkedList<String> ();
45
46                 try {
47                         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
48                 } catch (ErrorObjectNotFoundException e) {
49                         throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode()
50                                                                                  + " - update error.properties before using this exception code");
51                 }
52         }
53
54         /**
55          * Instantiates a new AAI exception.
56          *
57          * @param code the code
58          */
59         public AAIException(String code) {
60                 super();
61
62                 this.code = code;
63                 this.templateVars = new LinkedList<String> ();
64
65                 try {
66                         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
67                 } catch (ErrorObjectNotFoundException e) {
68                         throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode()
69                                                                                  + " - update error.properties before using this exception code");
70                 }
71         }
72         
73         /**
74          * Instantiates a new AAI exception.
75          *
76          * @param code the code
77          * @param details the details
78          */
79         public AAIException(String code, String details) {
80                 super(details);
81
82                 this.code = code;
83                 this.templateVars = new LinkedList<String> ();
84
85                 try {
86                         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
87                         errorObject.setDetails(details);
88                 } catch (ErrorObjectNotFoundException e) {
89                         throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode()
90                                                                                  + " - update error.properties before using this exception code");
91                 }
92         }
93
94         /**
95          * Instantiates a new AAI exception.
96          *
97          * @param code the code
98          * @param cause the cause
99          */
100         public AAIException(String code, Throwable cause) {
101                 super(cause);
102
103                 this.code = code;
104                 this.templateVars = new LinkedList<String> ();
105
106                 try {
107                         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
108                 } catch (ErrorObjectNotFoundException e) {
109                         throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode()
110                                                                                  + " - update error.properties before using this exception code");
111                 }
112         }
113         
114         /**
115          * Instantiates a new AAI exception.
116          *
117          * @param code the code
118          * @param cause the cause
119          * @param details the details
120          */
121         public AAIException(String code, Throwable cause, String details) {
122                 super(details, cause);
123
124                 this.code = code;
125                 this.templateVars = new LinkedList<String> ();
126
127                 try {
128                         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
129                 } catch (ErrorObjectNotFoundException e) {
130                         throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode()
131                                                                                  + " - update error.properties before using this exception code");
132                 }
133         }
134         
135         public String getCode() {
136                 return code;
137         }
138
139         public ErrorObject getErrorObject() {
140                 return errorObject;
141         }
142
143         public Collection<String> getTemplateVars() {
144                 return templateVars;
145         }
146 }