[pushover] Added exception handling and synchronized (#10437)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-04-04 18:58:16 +02:00 committed by GitHub
parent aad3641368
commit 15f69b9011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 13 deletions

View File

@ -31,6 +31,8 @@ Currently the binding does not support any Channels.
## Thing Actions ## Thing Actions
All actions return a `Boolean` value to indicate if the message was sent successfully or not. 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. 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. 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. One has to pass a `null` value if it should be skipped or the default value for it should be used.

View File

@ -37,7 +37,7 @@ import org.openhab.core.cache.ExpiringCacheMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -130,7 +130,7 @@ public class PushoverAPIConnection {
return executeRequest(HttpMethod.POST, url, body); 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 { throws PushoverCommunicationException, PushoverConfigurationException {
logger.trace("Pushover request: {} - URL = '{}'", httpMethod, url); logger.trace("Pushover request: {} - URL = '{}'", httpMethod, url);
try { try {
@ -169,13 +169,11 @@ public class PushoverAPIConnection {
private String getMessageError(String content) { private String getMessageError(String content) {
final JsonObject json = JsonParser.parseString(content).getAsJsonObject(); final JsonObject json = JsonParser.parseString(content).getAsJsonObject();
if (json.has("errors")) { final JsonElement errorsElement = json.get("errors");
final JsonArray errors = json.get("errors").getAsJsonArray(); if (errorsElement != null && errorsElement.isJsonArray()) {
if (errors != null) { return errorsElement.getAsJsonArray().toString();
return errors.toString();
}
} }
return "Unknown error occured."; return "@text/offline.conf-error-unknown";
} }
private boolean getMessageStatus(String content) { private boolean getMessageStatus(String content) {

View File

@ -31,8 +31,7 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
/** /**
* The {@link PushoverHandlerFactory} is responsible for creating things and thing * The {@link PushoverHandlerFactory} is responsible for creating things and thing handlers.
* handlers.
* *
* @author Christoph Weitkamp - Initial contribution * @author Christoph Weitkamp - Initial contribution
*/ */

View File

@ -134,7 +134,14 @@ public class PushoverAccountHandler extends BaseThingHandler {
public boolean sendMessage(PushoverMessageBuilder messageBuilder) { public boolean sendMessage(PushoverMessageBuilder messageBuilder) {
if (connection != null) { 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 { } else {
throw new IllegalArgumentException("PushoverAPIConnection is null!"); throw new IllegalArgumentException("PushoverAPIConnection is null!");
} }
@ -142,7 +149,14 @@ public class PushoverAccountHandler extends BaseThingHandler {
public String sendPriorityMessage(PushoverMessageBuilder messageBuilder) { public String sendPriorityMessage(PushoverMessageBuilder messageBuilder) {
if (connection != null) { 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 { } else {
throw new IllegalArgumentException("PushoverAPIConnection is null!"); throw new IllegalArgumentException("PushoverAPIConnection is null!");
} }
@ -150,12 +164,20 @@ public class PushoverAccountHandler extends BaseThingHandler {
public boolean cancelPriorityMessage(String receipt) { public boolean cancelPriorityMessage(String receipt) {
if (connection != null) { 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 { } else {
throw new IllegalArgumentException("PushoverAPIConnection is null!"); throw new IllegalArgumentException("PushoverAPIConnection is null!");
} }
} }
@SuppressWarnings("null")
private void asyncValidateUser() { private void asyncValidateUser() {
try { try {
connection.validateUser(); connection.validateUser();

View File

@ -1,6 +1,7 @@
# user defined messages # user defined messages
offline.conf-error-missing-apikey = The 'apikey' parameter must be configured. 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-missing-user = The 'user' parameter must be configured.
offline.conf-error-unknown = An unknown error occurred.
# actions # actions
sendMessageActionLabel = send a plain text message sendMessageActionLabel = send a plain text message

View File

@ -24,6 +24,7 @@ thing-type.config.pushover.pushover-account.expire.description = Dieser Paramete
# user defined messages # user defined messages
offline.conf-error-missing-apikey = Der Parameter 'apikey' muss konfiguriert werden. 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-missing-user = Der Parameter 'user' muss konfiguriert werden.
offline.conf-error-unknown = Ein unbekannter Fehler ist aufgetreten.
# actions # actions
sendMessageActionLabel = eine Textnachricht senden sendMessageActionLabel = eine Textnachricht senden