]> git.basschouten.com Git - openhab-addons.git/commitdiff
[influxdb] Handle exceptions gracefully (#15062)
authorJ-N-K <github@klug.nrw>
Tue, 13 Jun 2023 18:54:27 +0000 (20:54 +0200)
committerGitHub <noreply@github.com>
Tue, 13 Jun 2023 18:54:27 +0000 (20:54 +0200)
* [influxdb] Handle exceptions gracefully

Signed-off-by: Jan N. Klug <github@klug.nrw>
* also catch InfluxDBIOExceptions

Signed-off-by: Jan N. Klug <github@klug.nrw>
---------

Signed-off-by: Jan N. Klug <github@klug.nrw>
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java
bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/InfluxDB2RepositoryImpl.java

index ef66df495e683ef8e21e5a3c9db9c667086f7112..9449bbdac6b3f7dbf3f64534f298933f4f89b4a6 100644 (file)
@@ -30,6 +30,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.influxdb.InfluxDB;
 import org.influxdb.InfluxDBFactory;
+import org.influxdb.InfluxDBIOException;
 import org.influxdb.dto.BatchPoints;
 import org.influxdb.dto.Point;
 import org.influxdb.dto.Pong;
@@ -130,7 +131,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
             BatchPoints batchPoints = BatchPoints.database(configuration.getDatabaseName())
                     .retentionPolicy(configuration.getRetentionPolicy()).points(points).build();
             currentClient.write(batchPoints);
-        } catch (InfluxException e) {
+        } catch (InfluxException | InfluxDBIOException e) {
             logger.debug("Writing to database failed", e);
             return false;
         }
@@ -165,15 +166,19 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
 
     @Override
     public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
-        final InfluxDB currentClient = client;
-        if (currentClient != null) {
-            String query = queryCreator.createQuery(filter, retentionPolicy);
-            logger.trace("Query {}", query);
-            Query parsedQuery = new Query(query, configuration.getDatabaseName());
-            List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
-            return convertClientResultToRepository(results);
-        } else {
-            logger.warn("Returning empty list because queryAPI isn't present");
+        try {
+            final InfluxDB currentClient = client;
+            if (currentClient != null) {
+                String query = queryCreator.createQuery(filter, retentionPolicy);
+                logger.trace("Query {}", query);
+                Query parsedQuery = new Query(query, configuration.getDatabaseName());
+                List<QueryResult.Result> results = currentClient.query(parsedQuery, TimeUnit.MILLISECONDS).getResults();
+                return convertClientResultToRepository(results);
+            } else {
+                throw new InfluxException("API not present");
+            }
+        } catch (InfluxException | InfluxDBIOException e) {
+            logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
             return List.of();
         }
     }
index f675bf8241e5749d3d586fb985360a65fc6cd3bb..0b21da6b1276d161ce849341564b96d1dcb74f3e 100644 (file)
@@ -27,6 +27,7 @@ import java.util.stream.Stream;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.influxdb.InfluxDBIOException;
 import org.openhab.core.persistence.FilterCriteria;
 import org.openhab.persistence.influxdb.internal.FilterCriteriaQueryCreator;
 import org.openhab.persistence.influxdb.internal.InfluxDBConfiguration;
@@ -138,7 +139,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
             List<Point> clientPoints = influxPoints.stream().map(this::convertPointToClientFormat)
                     .filter(Optional::isPresent).map(Optional::get).toList();
             currentWriteAPI.writePoints(clientPoints);
-        } catch (InfluxException e) {
+        } catch (InfluxException | InfluxDBIOException e) {
             logger.debug("Writing to database failed", e);
             return false;
         }
@@ -173,7 +174,7 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
         try {
             deleteAPI.delete(start, stop, predicate, configuration.getRetentionPolicy(),
                     configuration.getDatabaseName());
-        } catch (InfluxException e) {
+        } catch (InfluxException | InfluxDBIOException e) {
             logger.debug("Deleting from database failed", e);
             return false;
         }
@@ -203,14 +204,18 @@ public class InfluxDB2RepositoryImpl implements InfluxDBRepository {
 
     @Override
     public List<InfluxRow> query(FilterCriteria filter, String retentionPolicy) {
-        final QueryApi currentQueryAPI = queryAPI;
-        if (currentQueryAPI != null) {
-            String query = queryCreator.createQuery(filter, retentionPolicy);
-            logger.trace("Query {}", query);
-            List<FluxTable> clientResult = currentQueryAPI.query(query);
-            return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
-        } else {
-            logger.warn("Returning empty list because queryAPI isn't present");
+        try {
+            final QueryApi currentQueryAPI = queryAPI;
+            if (currentQueryAPI != null) {
+                String query = queryCreator.createQuery(filter, retentionPolicy);
+                logger.trace("Query {}", query);
+                List<FluxTable> clientResult = currentQueryAPI.query(query);
+                return clientResult.stream().flatMap(this::mapRawResultToHistoric).toList();
+            } else {
+                throw new InfluxException("API not present");
+            }
+        } catch (InfluxException | InfluxDBIOException e) {
+            logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
             return List.of();
         }
     }