Better error handling

This commit is contained in:
Jason House
2020-08-06 17:02:48 +09:00
parent 80cd4a468e
commit f1e5851f59
9 changed files with 60 additions and 32 deletions

View File

@@ -41,9 +41,15 @@ public class NotificationController {
LOGGER.info("putTestAll()");
try {
notificationService.test();
LOGGER.info("Notification Test All Succeeded");
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED);
boolean result = notificationService.test();
if (result) {
LOGGER.info("Notification Test All Succeeded");
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED);
} else {
LOGGER.error("Notification Test All Failed");
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED);
}
} catch (Exception e) {
LOGGER.error("Notification Test All Failed", e);
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED.setExtras(e.getMessage()));
@@ -57,9 +63,14 @@ public class NotificationController {
LOGGER.info("putTest( {} )", id);
try {
notificationService.test(id);
LOGGER.info("Notification Test with ID {} Succeeded", id);
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED);
boolean result = notificationService.test(id);
if (result) {
LOGGER.info("Notification Test with ID {} Succeeded", id);
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED);
} else {
LOGGER.error("Notification Test Failed with ID {}", id);
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED);
}
} catch (Exception e) {
LOGGER.error(String.format("Notification Test Failed with ID %s", id), e);
return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED.setExtras(e.getMessage()));

View File

@@ -2,7 +2,6 @@ package com.jasonhhouse.gaps.notifications;
import com.jasonhhouse.gaps.NotificationType;
import com.jasonhhouse.gaps.service.IoService;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -47,7 +46,7 @@ public class EmailNotificationAgent implements NotificationAgent {
}
@Override
public void sendMessage(NotificationType notificationType, String level, String title, String message) {
public boolean sendMessage(NotificationType notificationType, String level, String title, String message) {
try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(mailFrom);
@@ -55,8 +54,10 @@ public class EmailNotificationAgent implements NotificationAgent {
simpleMailMessage.setSubject(title);
simpleMailMessage.setText(message);
mailSender.send(simpleMailMessage);
return true;
} catch (MailException e) {
LOGGER.error("Error with Sending Email Notification.", e);
return false;
}
}

View File

@@ -74,7 +74,7 @@ public class GotifyNotificationAgent implements NotificationAgent {
}
@Override
public void sendMessage(NotificationType notificationType, String level, String title, String message) {
public boolean sendMessage(NotificationType notificationType, String level, String title, String message) {
LOGGER.info("sendMessage( {}, {}, {} )", level, title, message);
HttpUrl url = HttpUrl.parse(String.format("%s/message?token=%s", address, token));
@@ -86,7 +86,7 @@ public class GotifyNotificationAgent implements NotificationAgent {
gotifyMessage = objectMapper.writeValueAsString(gotify);
} catch (JsonProcessingException e) {
LOGGER.error("Failed to turn Gotify message into JSON", e);
return;
return false;
}
LOGGER.info("Gotify {}", gotifyMessage);
@@ -100,16 +100,18 @@ public class GotifyNotificationAgent implements NotificationAgent {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
LOGGER.info("Gotify message sent via {}", url);
return true;
} else {
LOGGER.error("Error with Gotify Url: {} Body returned {}", url, response.body().toString());
return false;
}
} catch (IOException e) {
LOGGER.error(String.format("Error with Gotify Url: %s", url), e);
return false;
}
}
public static final class Gotify {
private final String title;
private final String message;

View File

@@ -22,5 +22,5 @@ public interface NotificationAgent {
boolean isEnabled();
void sendMessage(NotificationType notificationType, String level, String title, String message);
boolean sendMessage(NotificationType notificationType, String level, String title, String message);
}

View File

@@ -64,7 +64,7 @@ public class PushBulletNotificationAgent implements NotificationAgent {
}
@Override
public void sendMessage(NotificationType notificationType, String level, String title, String message) {
public boolean sendMessage(NotificationType notificationType, String level, String title, String message) {
LOGGER.info("sendMessage( {}, {}, {} )", level, title, message);
HttpUrl url = new HttpUrl.Builder()
@@ -89,7 +89,7 @@ public class PushBulletNotificationAgent implements NotificationAgent {
pushBulletMessage = objectMapper.writeValueAsString(pushBullet);
} catch (JsonProcessingException e) {
LOGGER.error("Failed to turn PushBullet message into JSON", e);
return;
return false;
}
LOGGER.info("pushBulletMessage {}", pushBulletMessage);
@@ -104,12 +104,15 @@ public class PushBulletNotificationAgent implements NotificationAgent {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
LOGGER.info("PushBullet message sent via {}", url);
return true;
} else {
LOGGER.error("Error with PushBullet Url: {} Body returned {}", url, response.body().toString());
return false;
}
} catch (IOException e) {
LOGGER.error(String.format("Error with PushBullet Url: %s", url), e);
return false;
}
}

View File

@@ -52,7 +52,7 @@ public class SlackNotificationAgent implements NotificationAgent {
}
@Override
public void sendMessage(NotificationType notificationType, String level, String title, String message) {
public boolean sendMessage(NotificationType notificationType, String level, String title, String message) {
LOGGER.info("sendMessage( {}, {}, {} )", level, title, message);
HttpUrl url = HttpUrl.get(webHookUrl);
@@ -68,7 +68,7 @@ public class SlackNotificationAgent implements NotificationAgent {
slackMessage = objectMapper.writeValueAsString(slack);
} catch (JsonProcessingException e) {
LOGGER.error("Failed to turn Slack message into JSON", e);
return;
return false;
}
LOGGER.info("slackMessage {}", slackMessage);
@@ -83,12 +83,15 @@ public class SlackNotificationAgent implements NotificationAgent {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
LOGGER.info("Slack message sent via {}", url);
return true;
} else {
LOGGER.error("Error with Slack Url: {} Body returned {}", url, response.body().toString());
return false;
}
} catch (IOException e) {
LOGGER.error(String.format("Error with Slack Url: %s", url), e);
return false;
}
}

View File

@@ -70,7 +70,7 @@ public class TelegramNotificationAgent implements NotificationAgent {
}
@Override
public void sendMessage(NotificationType notificationType, String level, String title, String message) {
public boolean sendMessage(NotificationType notificationType, String level, String title, String message) {
LOGGER.info("sendMessage( {}, {}, {} )", level, title, message);
HttpUrl url = new HttpUrl.Builder()
@@ -92,12 +92,15 @@ public class TelegramNotificationAgent implements NotificationAgent {
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful()) {
LOGGER.info("Telegram message sent via {}", url);
return true;
} else {
LOGGER.error("Error with Telegram Url: {} Body returned {}", url, response.body().toString());
return false;
}
} catch (IOException e) {
LOGGER.error(String.format("Error with Telegram Url: %s", url), e);
return false;
}
}

View File

@@ -23,7 +23,7 @@ public interface Notification {
void recommendedMoviesSearchFinished(PlexServer plexServer, PlexLibrary plexLibrary);
void test();
boolean test();
void test(int id) throws IllegalArgumentException, IllegalAccessException;
boolean test(int id) throws IllegalArgumentException, IllegalAccessException;
}

View File

@@ -38,7 +38,7 @@ public class NotificationService implements Notification {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST_PLEX_SERVER,"INFO", "Gaps Search", String.format("Connection to Plex Server %s Successful", plexServer.getFriendlyName())));
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST_PLEX_SERVER, "INFO", "Gaps Search", String.format("Connection to Plex Server %s Successful", plexServer.getFriendlyName())));
}
@Override
@@ -46,7 +46,7 @@ public class NotificationService implements Notification {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER,"INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Failed. %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error)));
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER, "INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Failed. %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error)));
}
@Override
@@ -54,7 +54,7 @@ public class NotificationService implements Notification {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER,"INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Successful", plexServer.getFriendlyName(), plexLibrary.getTitle())));
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER, "INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Successful", plexServer.getFriendlyName(), plexLibrary.getTitle())));
}
@Override
@@ -86,7 +86,7 @@ public class NotificationService implements Notification {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES,"INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Failed %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error)));
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES, "INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Failed %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error)));
}
@Override
@@ -94,23 +94,28 @@ public class NotificationService implements Notification {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES,"INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Successfully Finished", plexServer.getFriendlyName(), plexLibrary.getTitle())));
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES, "INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Successfully Finished", plexServer.getFriendlyName(), plexLibrary.getTitle())));
}
@Override
public void test() {
notificationAgents
.stream()
.filter(NotificationAgent::isEnabled)
.forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful"));
public boolean test() {
boolean passedTest = true;
for (NotificationAgent notificationAgent : notificationAgents) {
boolean result = notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful");
if (!result) {
passedTest = false;
}
}
return passedTest;
}
@Override
public void test(int id) throws IllegalArgumentException {
public boolean test(int id) throws IllegalArgumentException {
for (NotificationAgent notificationAgent : notificationAgents) {
if (notificationAgent.getId() == id) {
notificationAgent.sendMessage(NotificationType.TEST,"DEBUG", "Gaps Test", "Test Successful");
return;
return notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful");
}
}
throw new IllegalArgumentException("Invalid Id for Notification Agent");