[ecovacs] Handle invalid JSON responses properly (#16466)

The API has differing responses depending on device type. If our
understanding of the JSON format differs from that of the API, make sure
to properly set the thing OFFLINE and to log a meaningful message.

Related to #16187

Signed-off-by: Danny Baumann <dannybaumann@web.de>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
maniac103 2024-03-12 19:08:35 +01:00 committed by Ciprian Pascu
parent 1ad4ad5125
commit d0427f4814
2 changed files with 16 additions and 6 deletions

View File

@ -27,6 +27,11 @@ public class EcovacsApiException extends Exception {
this(reason, false);
}
public EcovacsApiException(String reason, Throwable cause) {
super(reason, cause);
isAuthFailure = false;
}
public EcovacsApiException(String reason, boolean isAuthFailure) {
super(reason);
this.isAuthFailure = isAuthFailure;

View File

@ -345,13 +345,18 @@ public final class EcovacsApiImpl implements EcovacsApi {
}
private <T> T handleResponse(ContentResponse response, Class<T> clazz) throws EcovacsApiException {
@Nullable
T respObject = gson.fromJson(response.getContentAsString(), clazz);
if (respObject == null) {
// should not happen in practice
throw new EcovacsApiException("No response received");
try {
@Nullable
T respObject = gson.fromJson(response.getContentAsString(), clazz);
if (respObject == null) {
// should not happen in practice
throw new EcovacsApiException("No response received");
}
return respObject;
} catch (JsonSyntaxException e) {
throw new EcovacsApiException("Failed to parse response '" + response.getContentAsString()
+ "' as data class " + clazz.getSimpleName(), e);
}
return respObject;
}
private Request createAuthRequest(String url, String clientKey, String clientSecret,