From ba0cf3fe1ccfc42d14216b2b5063d55897fb1e7d Mon Sep 17 00:00:00 2001 From: maniac103 Date: Tue, 12 Mar 2024 19:08:35 +0100 Subject: [PATCH] [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 --- .../internal/api/EcovacsApiException.java | 5 +++++ .../internal/api/impl/EcovacsApiImpl.java | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/EcovacsApiException.java b/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/EcovacsApiException.java index faee172ec92..b40e6450f11 100644 --- a/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/EcovacsApiException.java +++ b/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/EcovacsApiException.java @@ -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; diff --git a/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/impl/EcovacsApiImpl.java b/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/impl/EcovacsApiImpl.java index 431ff40803f..cfcf42fc34b 100644 --- a/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/impl/EcovacsApiImpl.java +++ b/bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/impl/EcovacsApiImpl.java @@ -345,13 +345,18 @@ public final class EcovacsApiImpl implements EcovacsApi { } private T handleResponse(ContentResponse response, Class 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,