]> git.basschouten.com Git - openhab-addons.git/blob
69710e10acac27b74ea9aefe326b1737a24f8dd4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tapocontrol.internal.helpers;
14
15 import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
16
17 import java.lang.reflect.Field;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants;
22
23 import com.google.gson.Gson;
24 import com.google.gson.JsonObject;
25
26 /**
27  * Class Handling TapoErrors
28  *
29  * @author Christian Wild - Initial contribution
30  */
31 @NonNullByDefault
32 public class TapoErrorHandler extends Exception {
33     private static final long serialVersionUID = 0L;
34     private Integer errorCode = 0;
35     private String errorMessage = "";
36     private String infoMessage = "";
37     private Gson gson = new Gson();
38
39     /**
40      * Constructor
41      *
42      */
43     public TapoErrorHandler() {
44     }
45
46     /**
47      * Constructor
48      * 
49      * @param errorCode error code (number)
50      */
51     public TapoErrorHandler(Integer errorCode) {
52         raiseError(errorCode);
53     }
54
55     /**
56      * Constructor
57      * 
58      * @param errorCode error code (number)
59      * @param infoMessage optional info-message
60      */
61     public TapoErrorHandler(Integer errorCode, String infoMessage) {
62         raiseError(errorCode, infoMessage);
63     }
64
65     /**
66      * Constructor
67      * 
68      * @param exception Exception
69      */
70     public TapoErrorHandler(Exception ex) {
71         raiseError(ex);
72     }
73
74     /**
75      * Constructor
76      * 
77      * @param exception Exception
78      * @param infoMessage optional info-message
79      */
80     public TapoErrorHandler(Exception ex, String infoMessage) {
81         raiseError(ex, infoMessage);
82     }
83
84     /***********************************
85      *
86      * Private Functions
87      *
88      ************************************/
89
90     /**
91      * GET ERROR-MESSAGE
92      * 
93      * @param errCode error Number (or constant ERR_CODE )
94      * @return error-message if set constant ERR_CODE_MSG. if not name of ERR_CODE is returned
95      */
96     private String getErrorMessage(Integer errCode) {
97         Field[] fields = TapoErrorConstants.class.getDeclaredFields();
98         /* loop ErrorConstants and search for code in value */
99         for (Field f : fields) {
100             String constName = f.getName();
101             try {
102                 Integer val = (Integer) f.get(this);
103                 if (val != null && val.equals(errCode)) {
104                     Field constantName = TapoErrorConstants.class.getDeclaredField(constName + "_MSG");
105                     String msg = getValueOrDefault(constantName.get(null), "").toString();
106                     if (msg.length() > 2) {
107                         return msg;
108                     } else {
109                         return infoMessage + " (" + constName + ")";
110                     }
111                 }
112             } catch (Exception e) {
113                 // next loop
114             }
115         }
116         return infoMessage + " (" + errCode.toString() + ")";
117     }
118
119     /***********************************
120      *
121      * Public Functions
122      *
123      ************************************/
124
125     /**
126      * Raises new error
127      * 
128      * @param errorCode error code (number)
129      */
130     public void raiseError(Integer errorCode) {
131         raiseError(errorCode, "");
132     }
133
134     /**
135      * Raises new error
136      * 
137      * @param errorCode error code (number)
138      * @param infoMessage optional info-message
139      */
140     public void raiseError(Integer errorCode, String infoMessage) {
141         this.errorCode = errorCode;
142         this.infoMessage = infoMessage;
143         this.errorMessage = getErrorMessage(errorCode);
144     }
145
146     /**
147      * Raises new error
148      * 
149      * @param exception Exception
150      */
151     public void raiseError(Exception ex) {
152         raiseError(ex, "");
153     }
154
155     /**
156      * Raises new error
157      * 
158      * @param exception Exception
159      * @param infoMessage optional info-message
160      */
161     public void raiseError(Exception ex, String infoMessage) {
162         this.errorCode = ex.hashCode();
163         this.infoMessage = infoMessage;
164         this.errorMessage = getValueOrDefault(ex.getMessage(), ex.toString());
165     }
166
167     /**
168      * Take over tapoError
169      * 
170      * @param tapoError
171      */
172     public void set(TapoErrorHandler tapoError) {
173         this.errorCode = tapoError.getNumber();
174         this.infoMessage = tapoError.getExtendedInfo();
175         this.errorMessage = getErrorMessage(this.errorCode);
176     }
177
178     /**
179      * Reset Error
180      */
181     public void reset() {
182         this.errorCode = 0;
183         this.errorMessage = "";
184         this.infoMessage = "";
185     }
186
187     /***********************************
188      *
189      * GET RESULTS
190      *
191      ************************************/
192
193     /**
194      * Get Error Message
195      * 
196      * @return error text
197      */
198     @Override
199     @Nullable
200     public String getMessage() {
201         return this.errorMessage;
202     }
203
204     /**
205      * Get Error Message directly by error-number
206      * 
207      * @param errorCode
208      * @return error message
209      */
210     public String getMessage(Integer errorCode) {
211         return getErrorMessage(errorCode);
212     }
213
214     /**
215      * Get Error Code
216      * 
217      * @return error code (integer)
218      */
219     public Integer getCode() {
220         return this.errorCode;
221     }
222
223     /**
224      * Get Info Message
225      * 
226      * @return error extended info
227      */
228     public String getExtendedInfo() {
229         return this.infoMessage;
230     }
231
232     /**
233      * Get Error Number
234      * 
235      * @return error number
236      */
237     public Integer getNumber() {
238         return this.errorCode;
239     }
240
241     /**
242      * Check if has Error
243      * 
244      * @return true if has error
245      */
246     public Boolean hasError() {
247         return this.errorCode != 0;
248     }
249
250     /**
251      * Get JSON-Object with errror
252      * 
253      * @return JsonObject with error-informations
254      */
255     public JsonObject getJson() {
256         JsonObject json;
257         json = gson.fromJson("{'error_code': '" + errorCode + "', 'error_message':'" + errorMessage + "'}",
258                 JsonObject.class);
259         if (json == null) {
260             json = new JsonObject();
261         }
262         return json;
263     }
264 }