From 3e318cca837aba115191d5fbaa72e9645ffa80c3 Mon Sep 17 00:00:00 2001 From: J-N-K Date: Tue, 13 Jun 2023 20:54:27 +0200 Subject: [PATCH] [influxdb] Handle exceptions gracefully (#15062) * [influxdb] Handle exceptions gracefully Signed-off-by: Jan N. Klug * also catch InfluxDBIOExceptions Signed-off-by: Jan N. Klug --------- Signed-off-by: Jan N. Klug Signed-off-by: Matt Myers --- .../influx1/InfluxDB1RepositoryImpl.java | 25 +++++++++++-------- .../influx2/InfluxDB2RepositoryImpl.java | 25 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java index ef66df495e683..9449bbdac6b3f 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java +++ b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx1/InfluxDB1RepositoryImpl.java @@ -30,6 +30,7 @@ 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 boolean write(List influxPoints) { 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 @@ private Optional convertPointToClientFormat(InfluxPoint point) { @Override public List 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 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 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(); } } diff --git a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/InfluxDB2RepositoryImpl.java b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/InfluxDB2RepositoryImpl.java index f675bf8241e57..0b21da6b1276d 100644 --- a/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/InfluxDB2RepositoryImpl.java +++ b/bundles/org.openhab.persistence.influxdb/src/main/java/org/openhab/persistence/influxdb/internal/influx2/InfluxDB2RepositoryImpl.java @@ -27,6 +27,7 @@ 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 boolean write(List influxPoints) { List 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 boolean remove(FilterCriteria filter) { 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 @@ private Optional convertPointToClientFormat(InfluxPoint point) { @Override public List query(FilterCriteria filter, String retentionPolicy) { - final QueryApi currentQueryAPI = queryAPI; - if (currentQueryAPI != null) { - String query = queryCreator.createQuery(filter, retentionPolicy); - logger.trace("Query {}", query); - List 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 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(); } }