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