Print exceptions for everything!!!

This commit is contained in:
Keir Nellyer 2016-07-09 18:09:26 -04:00
parent ac95a59f81
commit edf63dcafd
2 changed files with 136 additions and 27 deletions

View File

@ -1,5 +1,6 @@
package mineplex.core.report;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@ -11,7 +12,6 @@ import mineplex.core.chatsnap.command.PushSnapshotsCommand;
import mineplex.core.chatsnap.command.PushSnapshotsHandler;
import mineplex.core.command.CommandCenter;
import mineplex.core.common.jsonchat.JsonMessage;
import mineplex.core.common.util.BukkitFuture;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.portal.Portal;
@ -54,7 +54,7 @@ public class ReportManager
_serverName = serverName;
_serverWeight = serverWeight;
_reportRepository = new ReportRepository(this);
_reportRepository = new ReportRepository(this, _plugin.getLogger());
_reportProfileRepository = new ReportProfileRepository(plugin);
@ -205,7 +205,7 @@ public class ReportManager
if (reportCloser != null)
{
BukkitFuture.run(() ->
Bukkit.getScheduler().runTask(_plugin, () ->
{
Optional<String> reasonOptional = reportResult.getReason();
String prefix = getReportPrefix(reportId);
@ -222,6 +222,9 @@ public class ReportManager
String.format("/punish %s Report #%s - %s", suspectName, reportId, reason)));
});
}
}).exceptionally(throwable -> {
_plugin.getLogger().log(Level.SEVERE, "Post-report save failed.", throwable);
return null;
});
});
}
@ -348,7 +351,17 @@ public class ReportManager
*/
public CompletableFuture<Boolean> isHandlingReport(int accountId)
{
return _reportRepository.getReportsHandling(accountId).thenApply(reportIds -> {
CompletableFuture<List<Long>> future = _reportRepository.getReportsHandling(accountId);
return future.thenApply(reportIds -> {
// if for some reason we cannot fetch the report a user is handling
// assume the worst and return true
// this means we do not end up allowing a user to handle multiple reports simultaneously
if (reportIds == null)
{
return true;
}
for (long reportId : reportIds)
{
if (isActiveReport(reportId).join())
@ -358,9 +371,6 @@ public class ReportManager
}
return false;
}).exceptionally(throwable -> {
_plugin.getLogger().log(Level.SEVERE, "Error fetching reports player is handling.", throwable);
return true;
});
}
@ -378,7 +388,7 @@ public class ReportManager
*/
public CompletableFuture<Double> calculatePriority(Report report)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Double> future = CompletableFuture.supplyAsync(() ->
{
double priority = 0;
@ -397,6 +407,14 @@ public class ReportManager
return priority;
});
future.exceptionally(throwable ->
{
_plugin.getLogger().log(Level.SEVERE, "Error calculating report priority.", throwable);
return 1.0;
});
return future;
}
protected void onPlayerJoin(Player player)

View File

@ -19,6 +19,8 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import com.google.common.cache.Cache;
@ -94,20 +96,22 @@ public class ReportRepository
" WHERE id IN (%s);";
private final ReportManager _reportManager;
private final Logger _logger;
private final Cache<Long, Report> _cachedReports = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(5, TimeUnit.MINUTES)
.build();
public ReportRepository(ReportManager reportManager)
public ReportRepository(ReportManager reportManager, Logger logger)
{
_reportManager = reportManager;
_logger = logger;
}
public CompletableFuture<List<Integer>> getUnhandledReports()
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<List<Integer>> future = CompletableFuture.supplyAsync(() ->
{
List<Integer> unhandledReports = new ArrayList<>();
@ -128,11 +132,19 @@ public class ReportRepository
return unhandledReports;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error whilst fetching unhandled reports.", throwable);
return new ArrayList<>();
})
return future;
}
public CompletableFuture<List<Long>> getReportsHandling(int accountId)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<List<Long>> future = CompletableFuture.supplyAsync(() ->
{
List<Long> reportsHandling = new ArrayList<>();
@ -155,6 +167,14 @@ public class ReportRepository
return reportsHandling;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching reports being handled by specified account.", throwable);
return new ArrayList<>();
});
return future;
}
public CompletableFuture<List<Report>> getReports(Collection<Integer> reportIds)
@ -178,7 +198,7 @@ public class ReportRepository
}
else
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Report> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
@ -228,6 +248,14 @@ public class ReportRepository
return null;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching report.", throwable);
return null;
});
return future;
}
}
else
@ -236,14 +264,14 @@ public class ReportRepository
}
}
public CompletableFuture<Report> getOngoingReport(int suspectId)
public CompletableFuture<Report> getOngoingReport(int accountId)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Report> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GET_ONGOING_REPORT);
preparedStatement.setInt(1, suspectId);
preparedStatement.setInt(1, accountId);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next())
@ -264,17 +292,25 @@ public class ReportRepository
return null;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error getting ongoing report for account: " + accountId + ".", throwable);
return null;
});
return future;
}
public CompletableFuture<Optional<Integer>> getOngoingReport(int suspectId, ReportCategory reportCategory)
public CompletableFuture<Optional<Integer>> getOngoingReport(int accountId, ReportCategory category)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Optional<Integer>> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GET_ONGOING_REPORT_CATEGORY);
preparedStatement.setInt(1, suspectId);
preparedStatement.setInt(2, reportCategory.getId());
preparedStatement.setInt(1, accountId);
preparedStatement.setInt(2, category.getId());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next())
@ -289,11 +325,19 @@ public class ReportRepository
return Optional.empty();
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching ongoing report for account: " + accountId + ", category: " + category + ".", throwable);
return Optional.empty();
});
return future;
}
private CompletableFuture<Set<ReportMessage>> getReportReasons(long reportId)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Set<ReportMessage>> future = CompletableFuture.supplyAsync(() ->
{
Set<ReportMessage> reportMessages = new HashSet<>();
@ -322,16 +366,24 @@ public class ReportRepository
return reportMessages;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching report reasons for report: " + reportId + ".", throwable);
return new HashSet<>();
});
return future;
}
public CompletableFuture<Integer> getResultCount(int userId, ReportResultType reportResultType)
public CompletableFuture<Integer> getResultCount(int accountId, ReportResultType resultType)
{
return CompletableFuture.supplyAsync(() -> {
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
try (Connection connection = DBPool.getAccount().getConnection())
{
PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_RESULT_COUNT);
preparedStatement.setInt(1, userId);
preparedStatement.setInt(2, reportResultType.getId());
preparedStatement.setInt(1, accountId);
preparedStatement.setInt(2, resultType.getId());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next())
@ -346,11 +398,19 @@ public class ReportRepository
return null;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error fetching result count for account: " + accountId + ", type: " + resultType + ".", throwable);
return 0;
});
return future;
}
public CompletableFuture<Long> updateReport(Report report)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Long> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
@ -417,14 +477,29 @@ public class ReportRepository
setReportResultStatement.setTimestamp(4, new Timestamp(reportResult.getClosedTime().getTime())); // closed time
setReportResultStatement.execute();
}
return reportId;
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
});
future.exceptionally(throwable ->
{
Optional<Long> optional = report.getId();
_logger.log(Level.SEVERE,
"Error updating report" +
(optional.isPresent()
? " (#" + optional.get() + ")"
: ""
) + ".");
return null;
});
return future;
}
/**
@ -512,7 +587,7 @@ public class ReportRepository
public CompletableFuture<String> getAccountName(int accountId)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<String> future = CompletableFuture.supplyAsync(() ->
{
try (Connection connection = DBPool.getAccount().getConnection())
{
@ -532,6 +607,14 @@ public class ReportRepository
return null;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error whilst fetching name for account: " + accountId + ".");
return null;
});
return future;
}
public CompletableFuture<UUID> getAccountUUID(int accountId)
@ -542,7 +625,7 @@ public class ReportRepository
public CompletableFuture<Map<Integer, UUID>> getAccountUUIDs(Collection<Integer> accountIds)
{
return CompletableFuture.supplyAsync(() ->
CompletableFuture<Map<Integer, UUID>> future = CompletableFuture.supplyAsync(() ->
{
Map<Integer, UUID> accountUUIDs = new HashMap<>();
@ -566,5 +649,13 @@ public class ReportRepository
return accountUUIDs;
});
future.exceptionally(throwable ->
{
_logger.log(Level.SEVERE, "Error whilst fetching UUID(s).", throwable);
return new HashMap<>();
});
return future;
}
}