]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pushover] Added exception handling and synchronized (#10437)
authorChristoph Weitkamp <github@christophweitkamp.de>
Sun, 4 Apr 2021 16:58:16 +0000 (18:58 +0200)
committerGitHub <noreply@github.com>
Sun, 4 Apr 2021 16:58:16 +0000 (18:58 +0200)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
bundles/org.openhab.binding.pushover/README.md
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/connection/PushoverAPIConnection.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/factory/PushoverHandlerFactory.java
bundles/org.openhab.binding.pushover/src/main/java/org/openhab/binding/pushover/internal/handler/PushoverAccountHandler.java
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover.properties
bundles/org.openhab.binding.pushover/src/main/resources/OH-INF/i18n/pushover_de.properties

index f19e70bdfe8931cb29113596b3f66519de62106d..1719689689c9e7e0293ab5169fe7f950c37c10e9 100644 (file)
@@ -31,6 +31,8 @@ Currently the binding does not support any Channels.
 ## Thing Actions
 
 All actions return a `Boolean` value to indicate if the message was sent successfully or not.
+If the communication to Pushover servers fails the binding does not try to send the message again.
+One has to take care of that on its own if it is important.
 The parameter `message` is **mandatory**, the `title` parameter defaults to whatever value you defined in the `title` related configuration parameter.
 Parameters declared as `@Nullable` are not optional.
 One has to pass a `null` value if it should be skipped or the default value for it should be used.
index a7c7fef07e7ce20bbfa78e22f16aca30bc0ee997..8e8f6e820c1d31b37754e3ad952aec81399e8dc5 100644 (file)
@@ -37,7 +37,7 @@ import org.openhab.core.cache.ExpiringCacheMap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 
@@ -130,7 +130,7 @@ public class PushoverAPIConnection {
         return executeRequest(HttpMethod.POST, url, body);
     }
 
-    private String executeRequest(HttpMethod httpMethod, String url, @Nullable ContentProvider body)
+    private synchronized String executeRequest(HttpMethod httpMethod, String url, @Nullable ContentProvider body)
             throws PushoverCommunicationException, PushoverConfigurationException {
         logger.trace("Pushover request: {} - URL = '{}'", httpMethod, url);
         try {
@@ -169,13 +169,11 @@ public class PushoverAPIConnection {
 
     private String getMessageError(String content) {
         final JsonObject json = JsonParser.parseString(content).getAsJsonObject();
-        if (json.has("errors")) {
-            final JsonArray errors = json.get("errors").getAsJsonArray();
-            if (errors != null) {
-                return errors.toString();
-            }
+        final JsonElement errorsElement = json.get("errors");
+        if (errorsElement != null && errorsElement.isJsonArray()) {
+            return errorsElement.getAsJsonArray().toString();
         }
-        return "Unknown error occured.";
+        return "@text/offline.conf-error-unknown";
     }
 
     private boolean getMessageStatus(String content) {
index a08d95242f8250c39031099ba10f7561c136fe8e..2ce4f3cfa00490933fd1533aa673ab93352396f1 100644 (file)
@@ -31,8 +31,7 @@ import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 
 /**
- * The {@link PushoverHandlerFactory} is responsible for creating things and thing
- * handlers.
+ * The {@link PushoverHandlerFactory} is responsible for creating things and thing handlers.
  *
  * @author Christoph Weitkamp - Initial contribution
  */
index ae717907636a168072029edf08e5f30ed03b1ebd..9840bc23ae8c66a1841604d6eb75ead5d28f1c18 100644 (file)
@@ -134,7 +134,14 @@ public class PushoverAccountHandler extends BaseThingHandler {
 
     public boolean sendMessage(PushoverMessageBuilder messageBuilder) {
         if (connection != null) {
-            return connection.sendMessage(messageBuilder);
+            try {
+                return connection.sendMessage(messageBuilder);
+            } catch (PushoverCommunicationException e) {
+                // do nothing, causing exception is already logged
+            } catch (PushoverConfigurationException e) {
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
+            }
+            return false;
         } else {
             throw new IllegalArgumentException("PushoverAPIConnection is null!");
         }
@@ -142,7 +149,14 @@ public class PushoverAccountHandler extends BaseThingHandler {
 
     public String sendPriorityMessage(PushoverMessageBuilder messageBuilder) {
         if (connection != null) {
-            return connection.sendPriorityMessage(messageBuilder);
+            try {
+                return connection.sendPriorityMessage(messageBuilder);
+            } catch (PushoverCommunicationException e) {
+                // do nothing, causing exception is already logged
+            } catch (PushoverConfigurationException e) {
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
+            }
+            return "";
         } else {
             throw new IllegalArgumentException("PushoverAPIConnection is null!");
         }
@@ -150,12 +164,20 @@ public class PushoverAccountHandler extends BaseThingHandler {
 
     public boolean cancelPriorityMessage(String receipt) {
         if (connection != null) {
-            return connection.cancelPriorityMessage(receipt);
+            try {
+                return connection.cancelPriorityMessage(receipt);
+            } catch (PushoverCommunicationException e) {
+                // do nothing, causing exception is already logged
+            } catch (PushoverConfigurationException e) {
+                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
+            }
+            return false;
         } else {
             throw new IllegalArgumentException("PushoverAPIConnection is null!");
         }
     }
 
+    @SuppressWarnings("null")
     private void asyncValidateUser() {
         try {
             connection.validateUser();
index 5e1bad241dfbbfaae46d49623ce90cda9aae334e..85e66740a898562c49c5aa2982d1e063f9643d2d 100644 (file)
@@ -1,6 +1,7 @@
 # user defined messages
 offline.conf-error-missing-apikey = The 'apikey' parameter must be configured.
 offline.conf-error-missing-user = The 'user' parameter must be configured.
+offline.conf-error-unknown = An unknown error occurred.
 
 # actions
 sendMessageActionLabel = send a plain text message
index f340464ea0179b4f2f3cc5d28e89e40714b5f625..9f094e31e8f77315a140eeea86778af0c3f5d05a 100644 (file)
@@ -24,6 +24,7 @@ thing-type.config.pushover.pushover-account.expire.description = Dieser Paramete
 # user defined messages
 offline.conf-error-missing-apikey = Der Parameter 'apikey' muss konfiguriert werden.
 offline.conf-error-missing-user = Der Parameter 'user' muss konfiguriert werden.
+offline.conf-error-unknown = Ein unbekannter Fehler ist aufgetreten.
 
 # actions
 sendMessageActionLabel = eine Textnachricht senden