320d5630525cacc5aeccfbf39ea445abbba3060e
[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
23 package org.onap.aai.exceptions;
24
25 import java.util.Collection;
26 import java.util.LinkedList;
27
28 import org.onap.aai.logging.ErrorLogHelper;
29 import org.onap.aai.logging.ErrorObject;
30 import org.onap.aai.logging.ErrorObjectNotFoundException;
31
32 public class AAIException extends Exception {
33
34     private static final String UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE =
35             " - update error.properties before using this exception code";
36     private static final String FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE =
37             "Failed to instantiate AAIException with code=";
38     public static final String DEFAULT_EXCEPTION_CODE = "AAI_4000";
39     private static final long serialVersionUID = 1L;
40
41     private final String code;
42     private final ErrorObject errorObject;
43     private final Collection<String> templateVars;
44
45     /**
46      * Instantiates a new AAI exception.
47      */
48     public AAIException() {
49         super();
50         this.code = DEFAULT_EXCEPTION_CODE;
51         this.templateVars = new LinkedList<>();
52
53         try {
54             this.errorObject = ErrorLogHelper.getErrorObject(getCode());
55         } catch (ErrorObjectNotFoundException e) {
56             throw new RuntimeException(FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE + getCode()
57                     + UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE);
58         }
59     }
60
61     /**
62      * Instantiates a new AAI exception.
63      *
64      * @param code the code
65      */
66     public AAIException(String code) {
67         super();
68
69         this.code = code;
70         this.templateVars = new LinkedList<>();
71
72         try {
73             this.errorObject = ErrorLogHelper.getErrorObject(getCode());
74         } catch (ErrorObjectNotFoundException e) {
75             throw new RuntimeException(FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE + getCode()
76                     + UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE);
77         }
78     }
79
80     /**
81      * Instantiates a new AAI exception.
82      *
83      * @param code the code
84      * @param details the details
85      */
86     public AAIException(String code, String details) {
87         super(details);
88
89         this.code = code;
90         this.templateVars = new LinkedList<>();
91
92         try {
93             this.errorObject = ErrorLogHelper.getErrorObject(getCode());
94             errorObject.setDetails(details);
95         } catch (ErrorObjectNotFoundException e) {
96             throw new RuntimeException(FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE + getCode()
97                     + UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE);
98         }
99     }
100
101     /**
102      * Instantiates a new AAI exception.
103      *
104      * @param code the code
105      * @param cause the cause
106      */
107     public AAIException(String code, Throwable cause) {
108         super(cause);
109
110         this.code = code;
111         this.templateVars = new LinkedList<>();
112
113         try {
114             this.errorObject = ErrorLogHelper.getErrorObject(getCode());
115         } catch (ErrorObjectNotFoundException e) {
116             throw new RuntimeException(FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE + getCode()
117                     + UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE);
118         }
119     }
120
121     /**
122      * Instantiates a new AAI exception.
123      *
124      * @param code the code
125      * @param cause the cause
126      * @param details the details
127      */
128     public AAIException(String code, Throwable cause, String details) {
129         super(details, cause);
130
131         this.code = code;
132         this.templateVars = new LinkedList<>();
133
134         try {
135             this.errorObject = ErrorLogHelper.getErrorObject(getCode());
136         } catch (ErrorObjectNotFoundException e) {
137             throw new RuntimeException(FAILED_TO_INSTANTIATE_AAI_EXCEPTION_WITH_CODE + getCode()
138                     + UPDATE_ERROR_PROPERTIES_BEFORE_USING_THIS_EXCEPTION_CODE);
139         }
140     }
141
142     public String getCode() {
143         return code;
144     }
145
146     public ErrorObject getErrorObject() {
147         return errorObject;
148     }
149
150     public Collection<String> getTemplateVars() {
151         return templateVars;
152     }
153 }