Skip to content

Commit 58470ca

Browse files
authored
[tapocontrol] Moved error messages to i18n (openhab#14790)
* [tapocontrol] Moved error messages to i18n --------- Signed-off-by: Christian Wild <[email protected]>
1 parent 6ebfd84 commit 58470ca

File tree

11 files changed

+335
-260
lines changed

11 files changed

+335
-260
lines changed

bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoCloudConnector.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.openhab.binding.tapocontrol.internal.api;
1414

1515
import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16-
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
16+
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;
1717

1818
import java.util.UUID;
1919
import java.util.concurrent.TimeoutException;
@@ -139,11 +139,11 @@ private String getTokenFromResponse(ContentResponse response) {
139139
logger.trace("cloud returns error: '{}'", rBody);
140140
}
141141
} else {
142-
handleError(new TapoErrorHandler(ERR_JSON_DECODE_FAIL));
142+
handleError(new TapoErrorHandler(ERR_API_JSON_DECODE_FAIL));
143143
logger.trace("unexpected json-response '{}'", rBody);
144144
}
145145
} else {
146-
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE, ERR_HTTP_RESPONSE_MSG));
146+
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE));
147147
logger.warn("invalid response while login");
148148
token = "";
149149
}
@@ -229,7 +229,7 @@ protected ContentResponse sendCloudRequest(String url, String payload) {
229229
handleError(new TapoErrorHandler(e));
230230
} catch (TimeoutException e) {
231231
logger.debug("({}) sending request timeout: {}", uid, e.toString());
232-
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, e.toString()));
232+
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, e.toString()));
233233
} catch (Exception e) {
234234
logger.debug("({}) sending request failed: {}", uid, e.toString());
235235
handleError(new TapoErrorHandler(e));

bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceConnector.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.openhab.binding.tapocontrol.internal.api;
1414

1515
import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16-
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
16+
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;
1717
import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
1818
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.jsonObjectToInt;
1919

@@ -97,7 +97,7 @@ public boolean login() {
9797
return this.loggedIn();
9898
} else {
9999
logger.debug("({}) no ping while login '{}'", uid, this.ipAddress);
100-
handleError(new TapoErrorHandler(ERR_DEVICE_OFFLINE, "no ping while login"));
100+
handleError(new TapoErrorHandler(ERR_BINDING_DEVICE_OFFLINE, "no ping while login"));
101101
return false;
102102
}
103103
}
@@ -296,7 +296,7 @@ protected void sendSecurePasstrhroug(String payload, String command) {
296296
@Override
297297
protected void handleSuccessResponse(String responseBody) {
298298
JsonObject jsnResult = getJsonFromResponse(responseBody);
299-
Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_JSON_DECODE_FAIL);
299+
Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
300300
if (errorCode != 0) {
301301
logger.debug("({}) set deviceInfo not successful: {}", uid, jsnResult);
302302
this.device.handleConnectionState();
@@ -409,7 +409,7 @@ private JsonObject getJsonFromResponse(String responseBody) {
409409
}
410410
}
411411
logger.debug("({}) sendPayload exception {}", uid, responseBody);
412-
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE));
412+
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE));
413413
return new JsonObject();
414414
}
415415

@@ -440,7 +440,7 @@ public Boolean isOnline(Boolean raiseError) {
440440
} else {
441441
logger.trace("({}) device is offline (no ping)", uid);
442442
if (raiseError) {
443-
handleError(new TapoErrorHandler(ERR_DEVICE_OFFLINE));
443+
handleError(new TapoErrorHandler(ERR_BINDING_DEVICE_OFFLINE));
444444
}
445445
logout();
446446
return false;

bundles/org.openhab.binding.tapocontrol/src/main/java/org/openhab/binding/tapocontrol/internal/api/TapoDeviceHttpApi.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
package org.openhab.binding.tapocontrol.internal.api;
1414

1515
import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
16-
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
16+
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;
1717
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;
1818

1919
import java.util.concurrent.TimeUnit;
@@ -167,7 +167,7 @@ protected String createHandshake() {
167167
}
168168
} catch (Exception e) {
169169
logger.debug("({}) could not createHandshake: {}", uid, e.toString());
170-
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not createHandshake"));
170+
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not createHandshake"));
171171
}
172172
return cookie;
173173
}
@@ -186,7 +186,7 @@ private String getKeyFromResponse(ContentResponse response) {
186186
return jsonObjectToString(jsonObj.getAsJsonObject("result"), "key");
187187
} else {
188188
logger.warn("({}) could not getKeyFromResponse '{}'", uid, rBody);
189-
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not getKeyFromResponse"));
189+
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not getKeyFromResponse"));
190190
}
191191
return "";
192192
}
@@ -204,7 +204,7 @@ private String getCookieFromResponse(ContentResponse response) {
204204
logger.trace("({}) got cookie: '{}'", uid, cookie);
205205
} catch (Exception e) {
206206
logger.warn("({}) could not getCookieFromResponse", uid);
207-
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not getCookieFromResponse"));
207+
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not getCookieFromResponse"));
208208
}
209209
return cookie;
210210
}
@@ -258,7 +258,7 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
258258
/* get errocode (0=success) */
259259
JsonObject jsonObject = GSON.fromJson(decryptedResponse, JsonObject.class);
260260
if (jsonObject != null) {
261-
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
261+
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
262262
if (errorCode == 0) {
263263
/* return result if set / else request was successful */
264264
result = jsonObjectToString(jsonObject.getAsJsonObject("result"), "token");
@@ -269,11 +269,11 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
269269
}
270270
} else {
271271
logger.debug("({}) unexpected json-response '{}'", uid, decryptedResponse);
272-
tapoError.raiseError(ERR_JSON_ENCODE_FAIL, "could not get token");
272+
tapoError.raiseError(ERR_API_JSON_ENCODE_FAIL, "could not get token");
273273
}
274274
} else {
275275
logger.debug("({}) invalid response while login", uid);
276-
tapoError.raiseError(ERR_HTTP_RESPONSE, "invalid response while login");
276+
tapoError.raiseError(ERR_BINDING_HTTP_RESPONSE, "invalid response while login");
277277
}
278278
/* handle error */
279279
if (tapoError.hasError()) {
@@ -315,7 +315,7 @@ protected ContentResponse sendRequest(String url, String payload) {
315315
handleError(new TapoErrorHandler(e));
316316
} catch (TimeoutException e) {
317317
logger.debug("({}) sending request timeout: {}", uid, e.toString());
318-
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, e.toString()));
318+
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, e.toString()));
319319
} catch (Exception e) {
320320
logger.debug("({}) sending request failed: {}", uid, e.toString());
321321
handleError(new TapoErrorHandler(e));
@@ -355,14 +355,14 @@ public void onComplete(Result result) {
355355
String errorMessage = getValueOrDefault(e.getMessage(), "");
356356
if (e instanceof TimeoutException) {
357357
logger.debug("({}) sendAsyncRequest timeout'{}'", uid, errorMessage);
358-
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, errorMessage));
358+
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, errorMessage));
359359
} else {
360360
logger.debug("({}) sendAsyncRequest failed'{}'", uid, errorMessage);
361361
handleError(new TapoErrorHandler(new Exception(e), errorMessage));
362362
}
363363
} else if (response.getStatus() != 200) {
364364
logger.debug("({}) sendAsyncRequest response error'{}'", uid, response.getStatus());
365-
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE, getContentAsString()));
365+
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE, getContentAsString()));
366366
} else {
367367
/* request successful */
368368
String rBody = getContentAsString();
@@ -411,10 +411,10 @@ protected Integer getErrorCode(@Nullable ContentResponse response) {
411411
String responseBody = response.getContentAsString();
412412
return getErrorCode(responseBody);
413413
} else {
414-
return ERR_HTTP_RESPONSE;
414+
return ERR_BINDING_HTTP_RESPONSE.getCode();
415415
}
416416
} catch (Exception e) {
417-
return ERR_HTTP_RESPONSE;
417+
return ERR_BINDING_HTTP_RESPONSE.getCode();
418418
}
419419
}
420420

@@ -428,7 +428,7 @@ protected Integer getErrorCode(String responseBody) {
428428
try {
429429
JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class);
430430
/* get errocode (0=success) */
431-
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
431+
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
432432
if (errorCode == 0) {
433433
return 0;
434434
} else {
@@ -437,7 +437,7 @@ protected Integer getErrorCode(String responseBody) {
437437
return errorCode;
438438
}
439439
} catch (Exception e) {
440-
return ERR_HTTP_RESPONSE;
440+
return ERR_BINDING_HTTP_RESPONSE.getCode();
441441
}
442442
}
443443

@@ -451,7 +451,7 @@ protected Boolean hasErrorCode(String responseBody) {
451451
if (isValidJson(responseBody)) {
452452
JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class);
453453
/* get errocode (0=success) */
454-
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
454+
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
455455
if (errorCode > 0) {
456456
return true;
457457
}
@@ -491,7 +491,7 @@ protected String decryptResponse(String responseBody) {
491491
String encryptedResponse = jsonObjectToString(jsonObject.getAsJsonObject("result"), "response");
492492
return tapoCipher.decode(encryptedResponse);
493493
} else {
494-
handleError(new TapoErrorHandler(ERR_JSON_DECODE_FAIL));
494+
handleError(new TapoErrorHandler(ERR_API_JSON_DECODE_FAIL));
495495
}
496496
} catch (Exception ex) {
497497
logger.debug("({}) exception '{}' decryptingResponse: '{}'", uid, ex.toString(), responseBody);
@@ -549,7 +549,7 @@ public Boolean loggedIn(Boolean raiseError) {
549549
} else {
550550
logger.trace("({}) not logged in", uid);
551551
if (raiseError) {
552-
handleError(new TapoErrorHandler(ERR_LOGIN));
552+
handleError(new TapoErrorHandler(ERR_API_LOGIN));
553553
}
554554
return false;
555555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.constants;
14+
15+
import org.eclipse.jdt.annotation.NonNullByDefault;
16+
17+
/**
18+
* The {@link TapoErrorCode} enum lists known errorcodes can be received or thrown by binding
19+
*
20+
* @author Christian Wild - Initial contribution
21+
*/
22+
@NonNullByDefault
23+
public enum TapoErrorCode {
24+
NO_ERROR(0),
25+
ERR_UNKNOWN(-1, TapoErrorType.UNKNOWN),
26+
ERR_API_SESSION_TIMEOUT(9999, TapoErrorType.COMMUNICATION_RETRY),
27+
ERR_API_NULL_TRANSPORT(1000),
28+
ERR_API_REQUEST(1002),
29+
ERR_API_HAND_SHAKE_FAILED(1100, TapoErrorType.COMMUNICATION_RETRY),
30+
ERR_API_LOGIN_FAILED(1111),
31+
ERR_API_HTTP_TRANSPORT_FAILED(1112),
32+
ERR_API_MULTI_REQUEST_FAILED(1200),
33+
ERR_API_JSON_DECODE_FAIL(-1003),
34+
ERR_API_JSON_ENCODE_FAIL(-1004),
35+
ERR_API_AES_DECODE_FAIL(-1005),
36+
ERR_API_REQUEST_LEN_ERROR(-1006),
37+
ERR_API_CLOUD_FAILED(-1007),
38+
ERR_API_PARAMS(-1008),
39+
ERR_API_RSA_KEY_LENGTH(-1010),
40+
ERR_API_SESSION_PARAM(-1101),
41+
ERR_API_QUICK_SETUP(-1201),
42+
ERR_API_DEVICE(-1301),
43+
ERR_API_DEVICE_NEXT_EVENT(-1302),
44+
ERR_API_FIRMWARE(-1401),
45+
ERR_API_FIRMWARE_VER_ERROR(-1402),
46+
ERR_API_LOGIN(-1501),
47+
ERR_API_TIME(-1601),
48+
ERR_API_TIME_SYS(-1602),
49+
ERR_API_TIME_SAVE(-1603),
50+
ERR_API_WIRELESS(-1701),
51+
ERR_API_WIRELESS_UNSUPPORTED(-1702),
52+
ERR_API_SCHEDULE(-1801),
53+
ERR_API_SCHEDULE_FULL(-1802),
54+
ERR_API_SCHEDULE_CONFLICT(-1803),
55+
ERR_API_SCHEDULE_SAVE(-1804),
56+
ERR_API_SCHEDULE_INDEX(-1805),
57+
ERR_API_COUNTDOWN(-1901),
58+
ERR_API_COUNTDOWN_CONFLICT(-1902),
59+
ERR_API_COUNTDOWN_SAVE(-1903),
60+
ERR_API_ANTITHEFT(-2001),
61+
ERR_API_ANTITHEFT_CONFLICT(-2002),
62+
ERR_API_ANTITHEFT_SAVE(-2003),
63+
ERR_API_ACCOUNT(-2101),
64+
ERR_API_STAT(-2201),
65+
ERR_API_STAT_SAVE(-2202),
66+
ERR_API_DST(-2301),
67+
ERR_API_DST_SAVE(-2302),
68+
69+
// List of Cloud-ErrorCodes
70+
ERR_CLOUD_API_RATE(-20004),
71+
ERR_CLOUD_CREDENTIALS(-20601),
72+
ERR_CLOUD_JSON_FORMAT(-10100),
73+
ERR_CLOUD_METHOD_MISSING(-20103),
74+
ERR_CLOUD_PARAMETER_MISSING(-20104),
75+
ERR_CLOUD_TOKEN_EXPIRED(-20651),
76+
77+
// List of Binding-ErrorCodes
78+
ERR_BINDING_HTTP_RESPONSE(9001, TapoErrorType.COMMUNICATION_ERROR),
79+
ERR_BINDING_COOKIE(9002, TapoErrorType.COMMUNICATION_ERROR),
80+
ERR_BINDING_CREDENTIALS(9003, TapoErrorType.CONFIGURATION_ERROR),
81+
ERR_BINDING_DEVICE_OFFLINE(9009, TapoErrorType.COMMUNICATION_ERROR),
82+
ERR_BINDING_CONNECT_TIMEOUT(9010, TapoErrorType.COMMUNICATION_ERROR),
83+
84+
// List of Binding-Config-ErrorCodes
85+
ERR_CONFIG_IP(10001, TapoErrorType.CONFIGURATION_ERROR), // ip not set
86+
ERR_CONFIG_CREDENTIALS(10002, TapoErrorType.CONFIGURATION_ERROR), // credentials not set
87+
ERR_CONFIG_NO_BRIDGE(10003, TapoErrorType.CONFIGURATION_ERROR); // no bridge configured
88+
89+
private Integer code;
90+
private TapoErrorType errorType;
91+
92+
/* set code */
93+
private TapoErrorCode(Integer code) {
94+
this.code = code;
95+
this.errorType = TapoErrorType.GENERAL;
96+
}
97+
98+
private TapoErrorCode(Integer code, TapoErrorType errorType) {
99+
this.code = code;
100+
this.errorType = errorType;
101+
}
102+
103+
/* get vlaues */
104+
public Integer getCode() {
105+
return this.code;
106+
}
107+
108+
public TapoErrorType getType() {
109+
return this.errorType;
110+
}
111+
112+
public static TapoErrorCode fromCode(int errorCode) {
113+
for (TapoErrorCode e : TapoErrorCode.values()) {
114+
if (e.code.equals(errorCode))
115+
return e;
116+
}
117+
return ERR_UNKNOWN;
118+
}
119+
}

0 commit comments

Comments
 (0)