From 98b0cd7df5d28f2da92594e0ee75a08660b2cad5 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Wed, 17 Mar 2021 15:00:04 +0100 Subject: [PATCH] Gracefully handle JsonSyntaxException Signed-off-by: Christoph Weitkamp --- .../binding/sensebox/internal/SenseBoxAPIConnection.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.sensebox/src/main/java/org/openhab/binding/sensebox/internal/SenseBoxAPIConnection.java b/bundles/org.openhab.binding.sensebox/src/main/java/org/openhab/binding/sensebox/internal/SenseBoxAPIConnection.java index 907fa8782e8a6..0e14c975b9c8c 100644 --- a/bundles/org.openhab.binding.sensebox/src/main/java/org/openhab/binding/sensebox/internal/SenseBoxAPIConnection.java +++ b/bundles/org.openhab.binding.sensebox/src/main/java/org/openhab/binding/sensebox/internal/SenseBoxAPIConnection.java @@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory; import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; /** * The {@link SenseBoxAPIConnection} is responsible for fetching data from the senseBox API server. @@ -63,8 +64,9 @@ public SenseBoxData reallyFetchDataFromServer(String senseBoxId) { // the caching layer does not like null values SenseBoxData result = new SenseBoxData(); + String body = null; try { - String body = HttpUtil.executeUrl(METHOD, query, HEADERS, null, null, TIMEOUT); + body = HttpUtil.executeUrl(METHOD, query, HEADERS, null, null, TIMEOUT); logger.trace("Fetched Data: {}", body); SenseBoxData parsedData = gson.fromJson(body, SenseBoxData.class); @@ -148,6 +150,10 @@ public SenseBoxData reallyFetchDataFromServer(String senseBoxId) { logger.trace("================================="); result = parsedData; + } catch (JsonSyntaxException e) { + logger.debug("An error occurred while parsing the data into desired class: {} / {} / {}", body, + SenseBoxData.class.getName(), e.getMessage()); + result.setStatus(ThingStatus.OFFLINE); } catch (IOException e) { logger.debug("IO problems while fetching data: {} / {}", query, e.getMessage()); result.setStatus(ThingStatus.OFFLINE);