Skip to content

Commit

Permalink
[tapocontrol] Defined error messages in i18n
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Wild <[email protected]>
  • Loading branch information
wildcs committed Apr 17, 2023
1 parent aee4548 commit 45ca75f
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 170 deletions.
Original file line number Diff line number Diff line change
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 @@ -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);
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 @@ -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);
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;
}
} catch (Exception e) {
return ERR_HTTP_RESPONSE;
return ERR_BINDING_HTTP_RESPONSE;
}
}

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);
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;
}
}

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);
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
Loading

0 comments on commit 45ca75f

Please sign in to comment.