Refactor ErrorLogHelper
[aai/aai-common.git] / aai-els-onap-logging / 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  *  Modifications Copyright (C) 2023 Deutsche Telekom SA.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *    http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.aai.exceptions;
25
26 import java.util.Collection;
27 import java.util.LinkedList;
28
29 import org.onap.aai.logging.ErrorLogHelper;
30 import org.onap.aai.logging.ErrorObject;
31 import org.onap.aai.logging.ErrorObjectNotFoundException;
32
33 public class AAIException extends Exception {
34
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<>();
49
50         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
51     }
52
53     /**
54      * Instantiates a new AAI exception.
55      *
56      * @param code the code
57      */
58     public AAIException(String code) {
59         super();
60
61         this.code = code;
62         this.templateVars = new LinkedList<>();
63         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
64     }
65
66     /**
67      * Instantiates a new AAI exception.
68      *
69      * @param code the code
70      * @param details the details
71      */
72     public AAIException(String code, String details) {
73         super(details);
74
75         this.code = code;
76         this.templateVars = new LinkedList<>();
77         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
78         errorObject.setDetails(details);
79     }
80
81     /**
82      * Instantiates a new AAI exception.
83      *
84      * @param code the code
85      * @param cause the cause
86      */
87     public AAIException(String code, Throwable cause) {
88         super(cause);
89
90         this.code = code;
91         this.templateVars = new LinkedList<>();
92         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
93     }
94
95     /**
96      * Instantiates a new AAI exception.
97      *
98      * @param code the code
99      * @param cause the cause
100      * @param details the details
101      */
102     public AAIException(String code, Throwable cause, String details) {
103         super(details, cause);
104
105         this.code = code;
106         this.templateVars = new LinkedList<>();
107         this.errorObject = ErrorLogHelper.getErrorObject(getCode());
108     }
109
110     public String getCode() {
111         return code;
112     }
113
114     public ErrorObject getErrorObject() {
115         return errorObject;
116     }
117
118     public Collection<String> getTemplateVars() {
119         return templateVars;
120     }
121 }