]> git.basschouten.com Git - openhab-addons.git/commitdiff
[ecovacs] Handle invalid JSON responses properly (#16466)
authormaniac103 <dannybaumann@web.de>
Tue, 12 Mar 2024 18:08:35 +0000 (19:08 +0100)
committerGitHub <noreply@github.com>
Tue, 12 Mar 2024 18:08:35 +0000 (19:08 +0100)
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>
bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/EcovacsApiException.java
bundles/org.openhab.binding.ecovacs/src/main/java/org/openhab/binding/ecovacs/internal/api/impl/EcovacsApiImpl.java

index faee172ec92e313548428e7f36051d46cc49d91a..b40e6450f116cb2b799b258f03c10bc3e334532c 100644 (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;
index 431ff40803f61e4c2281fb6b8515cd1ede753416..cfcf42fc34b24aa867882df5f8b2b33514f4a4e2 100644 (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,