Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tapocontrol] Move error messages to i18n #14790

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.openhab.binding.tapocontrol.internal.api;

import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;

import java.util.UUID;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -139,11 +139,11 @@ private String getTokenFromResponse(ContentResponse response) {
logger.trace("cloud returns error: '{}'", rBody);
}
} else {
handleError(new TapoErrorHandler(ERR_JSON_DECODE_FAIL));
handleError(new TapoErrorHandler(ERR_API_JSON_DECODE_FAIL));
logger.trace("unexpected json-response '{}'", rBody);
}
} else {
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE, ERR_HTTP_RESPONSE_MSG));
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE));
logger.warn("invalid response while login");
token = "";
}
Expand Down Expand Up @@ -229,7 +229,7 @@ protected ContentResponse sendCloudRequest(String url, String payload) {
handleError(new TapoErrorHandler(e));
} catch (TimeoutException e) {
logger.debug("({}) sending request timeout: {}", uid, e.toString());
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, e.toString()));
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, e.toString()));
} catch (Exception e) {
logger.debug("({}) sending request failed: {}", uid, e.toString());
handleError(new TapoErrorHandler(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.openhab.binding.tapocontrol.internal.api;

import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.jsonObjectToInt;

Expand Down Expand Up @@ -97,7 +97,7 @@ public boolean login() {
return this.loggedIn();
} else {
logger.debug("({}) no ping while login '{}'", uid, this.ipAddress);
handleError(new TapoErrorHandler(ERR_DEVICE_OFFLINE, "no ping while login"));
handleError(new TapoErrorHandler(ERR_BINDING_DEVICE_OFFLINE, "no ping while login"));
return false;
}
}
Expand Down Expand Up @@ -296,7 +296,7 @@ protected void sendSecurePasstrhroug(String payload, String command) {
@Override
protected void handleSuccessResponse(String responseBody) {
JsonObject jsnResult = getJsonFromResponse(responseBody);
Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_JSON_DECODE_FAIL);
Integer errorCode = jsonObjectToInt(jsnResult, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
if (errorCode != 0) {
logger.debug("({}) set deviceInfo not successful: {}", uid, jsnResult);
this.device.handleConnectionState();
Expand Down Expand Up @@ -409,7 +409,7 @@ private JsonObject getJsonFromResponse(String responseBody) {
}
}
logger.debug("({}) sendPayload exception {}", uid, responseBody);
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE));
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE));
return new JsonObject();
}

Expand Down Expand Up @@ -440,7 +440,7 @@ public Boolean isOnline(Boolean raiseError) {
} else {
logger.trace("({}) device is offline (no ping)", uid);
if (raiseError) {
handleError(new TapoErrorHandler(ERR_DEVICE_OFFLINE));
handleError(new TapoErrorHandler(ERR_BINDING_DEVICE_OFFLINE));
}
logout();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package org.openhab.binding.tapocontrol.internal.api;

import static org.openhab.binding.tapocontrol.internal.constants.TapoBindingSettings.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorConstants.*;
import static org.openhab.binding.tapocontrol.internal.constants.TapoErrorCode.*;
import static org.openhab.binding.tapocontrol.internal.helpers.TapoUtils.*;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -167,7 +167,7 @@ protected String createHandshake() {
}
} catch (Exception e) {
logger.debug("({}) could not createHandshake: {}", uid, e.toString());
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not createHandshake"));
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not createHandshake"));
}
return cookie;
}
Expand All @@ -186,7 +186,7 @@ private String getKeyFromResponse(ContentResponse response) {
return jsonObjectToString(jsonObj.getAsJsonObject("result"), "key");
} else {
logger.warn("({}) could not getKeyFromResponse '{}'", uid, rBody);
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not getKeyFromResponse"));
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not getKeyFromResponse"));
}
return "";
}
Expand All @@ -204,7 +204,7 @@ private String getCookieFromResponse(ContentResponse response) {
logger.trace("({}) got cookie: '{}'", uid, cookie);
} catch (Exception e) {
logger.warn("({}) could not getCookieFromResponse", uid);
handleError(new TapoErrorHandler(ERR_HAND_SHAKE_FAILED, "could not getCookieFromResponse"));
handleError(new TapoErrorHandler(ERR_API_HAND_SHAKE_FAILED, "could not getCookieFromResponse"));
}
return cookie;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
/* get errocode (0=success) */
JsonObject jsonObject = GSON.fromJson(decryptedResponse, JsonObject.class);
if (jsonObject != null) {
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
if (errorCode == 0) {
/* return result if set / else request was successful */
result = jsonObjectToString(jsonObject.getAsJsonObject("result"), "token");
Expand All @@ -269,11 +269,11 @@ private String getTokenFromResponse(@Nullable ContentResponse response) {
}
} else {
logger.debug("({}) unexpected json-response '{}'", uid, decryptedResponse);
tapoError.raiseError(ERR_JSON_ENCODE_FAIL, "could not get token");
tapoError.raiseError(ERR_API_JSON_ENCODE_FAIL, "could not get token");
}
} else {
logger.debug("({}) invalid response while login", uid);
tapoError.raiseError(ERR_HTTP_RESPONSE, "invalid response while login");
tapoError.raiseError(ERR_BINDING_HTTP_RESPONSE, "invalid response while login");
}
/* handle error */
if (tapoError.hasError()) {
Expand Down Expand Up @@ -315,7 +315,7 @@ protected ContentResponse sendRequest(String url, String payload) {
handleError(new TapoErrorHandler(e));
} catch (TimeoutException e) {
logger.debug("({}) sending request timeout: {}", uid, e.toString());
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, e.toString()));
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, e.toString()));
} catch (Exception e) {
logger.debug("({}) sending request failed: {}", uid, e.toString());
handleError(new TapoErrorHandler(e));
Expand Down Expand Up @@ -355,14 +355,14 @@ public void onComplete(Result result) {
String errorMessage = getValueOrDefault(e.getMessage(), "");
if (e instanceof TimeoutException) {
logger.debug("({}) sendAsyncRequest timeout'{}'", uid, errorMessage);
handleError(new TapoErrorHandler(ERR_CONNECT_TIMEOUT, errorMessage));
handleError(new TapoErrorHandler(ERR_BINDING_CONNECT_TIMEOUT, errorMessage));
} else {
logger.debug("({}) sendAsyncRequest failed'{}'", uid, errorMessage);
handleError(new TapoErrorHandler(new Exception(e), errorMessage));
}
} else if (response.getStatus() != 200) {
logger.debug("({}) sendAsyncRequest response error'{}'", uid, response.getStatus());
handleError(new TapoErrorHandler(ERR_HTTP_RESPONSE, getContentAsString()));
handleError(new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE, getContentAsString()));
} else {
/* request successful */
String rBody = getContentAsString();
Expand Down Expand Up @@ -411,10 +411,10 @@ protected Integer getErrorCode(@Nullable ContentResponse response) {
String responseBody = response.getContentAsString();
return getErrorCode(responseBody);
} else {
return ERR_HTTP_RESPONSE;
return ERR_BINDING_HTTP_RESPONSE.getCode();
}
} catch (Exception e) {
return ERR_HTTP_RESPONSE;
return ERR_BINDING_HTTP_RESPONSE.getCode();
}
}

Expand All @@ -428,7 +428,7 @@ protected Integer getErrorCode(String responseBody) {
try {
JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class);
/* get errocode (0=success) */
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
if (errorCode == 0) {
return 0;
} else {
Expand All @@ -437,7 +437,7 @@ protected Integer getErrorCode(String responseBody) {
return errorCode;
}
} catch (Exception e) {
return ERR_HTTP_RESPONSE;
return ERR_BINDING_HTTP_RESPONSE.getCode();
}
}

Expand All @@ -451,7 +451,7 @@ protected Boolean hasErrorCode(String responseBody) {
if (isValidJson(responseBody)) {
JsonObject jsonObject = GSON.fromJson(responseBody, JsonObject.class);
/* get errocode (0=success) */
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_JSON_DECODE_FAIL);
Integer errorCode = jsonObjectToInt(jsonObject, "error_code", ERR_API_JSON_DECODE_FAIL.getCode());
if (errorCode > 0) {
return true;
}
Expand Down Expand Up @@ -491,7 +491,7 @@ protected String decryptResponse(String responseBody) {
String encryptedResponse = jsonObjectToString(jsonObject.getAsJsonObject("result"), "response");
return tapoCipher.decode(encryptedResponse);
} else {
handleError(new TapoErrorHandler(ERR_JSON_DECODE_FAIL));
handleError(new TapoErrorHandler(ERR_API_JSON_DECODE_FAIL));
}
} catch (Exception ex) {
logger.debug("({}) exception '{}' decryptingResponse: '{}'", uid, ex.toString(), responseBody);
Expand Down Expand Up @@ -549,7 +549,7 @@ public Boolean loggedIn(Boolean raiseError) {
} else {
logger.trace("({}) not logged in", uid);
if (raiseError) {
handleError(new TapoErrorHandler(ERR_LOGIN));
handleError(new TapoErrorHandler(ERR_API_LOGIN));
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tapocontrol.internal.constants;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link TapoErrorCode} enum lists known errorcodes can be received or thrown by binding
*
* @author Christian Wild - Initial contribution
*/
@NonNullByDefault
public enum TapoErrorCode {
NO_ERROR(0),
ERR_UNKNOWN(-1, TapoErrorType.UNKNOWN),
ERR_API_SESSION_TIMEOUT(9999, TapoErrorType.COMMUNICATION_RETRY),
ERR_API_NULL_TRANSPORT(1000),
ERR_API_REQUEST(1002),
ERR_API_HAND_SHAKE_FAILED(1100, TapoErrorType.COMMUNICATION_RETRY),
ERR_API_LOGIN_FAILED(1111),
ERR_API_HTTP_TRANSPORT_FAILED(1112),
ERR_API_MULTI_REQUEST_FAILED(1200),
ERR_API_JSON_DECODE_FAIL(-1003),
ERR_API_JSON_ENCODE_FAIL(-1004),
ERR_API_AES_DECODE_FAIL(-1005),
ERR_API_REQUEST_LEN_ERROR(-1006),
ERR_API_CLOUD_FAILED(-1007),
ERR_API_PARAMS(-1008),
ERR_API_RSA_KEY_LENGTH(-1010),
ERR_API_SESSION_PARAM(-1101),
ERR_API_QUICK_SETUP(-1201),
ERR_API_DEVICE(-1301),
ERR_API_DEVICE_NEXT_EVENT(-1302),
ERR_API_FIRMWARE(-1401),
ERR_API_FIRMWARE_VER_ERROR(-1402),
ERR_API_LOGIN(-1501),
ERR_API_TIME(-1601),
ERR_API_TIME_SYS(-1602),
ERR_API_TIME_SAVE(-1603),
ERR_API_WIRELESS(-1701),
ERR_API_WIRELESS_UNSUPPORTED(-1702),
ERR_API_SCHEDULE(-1801),
ERR_API_SCHEDULE_FULL(-1802),
ERR_API_SCHEDULE_CONFLICT(-1803),
ERR_API_SCHEDULE_SAVE(-1804),
ERR_API_SCHEDULE_INDEX(-1805),
ERR_API_COUNTDOWN(-1901),
ERR_API_COUNTDOWN_CONFLICT(-1902),
ERR_API_COUNTDOWN_SAVE(-1903),
ERR_API_ANTITHEFT(-2001),
ERR_API_ANTITHEFT_CONFLICT(-2002),
ERR_API_ANTITHEFT_SAVE(-2003),
ERR_API_ACCOUNT(-2101),
ERR_API_STAT(-2201),
ERR_API_STAT_SAVE(-2202),
ERR_API_DST(-2301),
ERR_API_DST_SAVE(-2302),

// List of Cloud-ErrorCodes
ERR_CLOUD_API_RATE(-20004),
ERR_CLOUD_CREDENTIALS(-20601),
ERR_CLOUD_JSON_FORMAT(-10100),
ERR_CLOUD_METHOD_MISSING(-20103),
ERR_CLOUD_PARAMETER_MISSING(-20104),
ERR_CLOUD_TOKEN_EXPIRED(-20651),

// List of Binding-ErrorCodes
ERR_BINDING_HTTP_RESPONSE(9001, TapoErrorType.COMMUNICATION_ERROR),
ERR_BINDING_COOKIE(9002, TapoErrorType.COMMUNICATION_ERROR),
ERR_BINDING_CREDENTIALS(9003, TapoErrorType.CONFIGURATION_ERROR),
ERR_BINDING_DEVICE_OFFLINE(9009, TapoErrorType.COMMUNICATION_ERROR),
ERR_BINDING_CONNECT_TIMEOUT(9010, TapoErrorType.COMMUNICATION_ERROR),

// List of Binding-Config-ErrorCodes
ERR_CONFIG_IP(10001, TapoErrorType.CONFIGURATION_ERROR), // ip not set
ERR_CONFIG_CREDENTIALS(10002, TapoErrorType.CONFIGURATION_ERROR), // credentials not set
ERR_CONFIG_NO_BRIDGE(10003, TapoErrorType.CONFIGURATION_ERROR); // no bridge configured

private Integer code;
private TapoErrorType errorType;

/* set code */
private TapoErrorCode(Integer code) {
this.code = code;
this.errorType = TapoErrorType.GENERAL;
}

private TapoErrorCode(Integer code, TapoErrorType errorType) {
this.code = code;
this.errorType = errorType;
}

/* get vlaues */
public Integer getCode() {
return this.code;
}

public TapoErrorType getType() {
return this.errorType;
}

public static TapoErrorCode fromCode(int errorCode) {
for (TapoErrorCode e : TapoErrorCode.values()) {
if (e.code.equals(errorCode))
return e;
}
return ERR_UNKNOWN;
}
}
Loading