Made the exception handler more reliable

This commit is contained in:
Norbi Peti 2016-11-03 22:45:10 +01:00
parent 09072e41e4
commit 4c10dfbf33
2 changed files with 38 additions and 1 deletions

View file

@ -11,7 +11,9 @@ import java.nio.file.CopyOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -106,6 +108,8 @@ public final class TBMCCoreAPI {
return body; return body;
} }
private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>();
/** /**
* Send exception to the {@link TBMCExceptionEvent}. * Send exception to the {@link TBMCExceptionEvent}.
* *
@ -115,7 +119,11 @@ public final class TBMCCoreAPI {
* The exception to send * The exception to send
*/ */
public static void SendException(String sourcemsg, Throwable e) { public static void SendException(String sourcemsg, Throwable e) {
Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); SendUnsentExceptions();
TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e);
Bukkit.getPluginManager().callEvent(event);
if (!event.isHandled())
exceptionsToSend.put(sourcemsg, e);
Bukkit.getLogger().warning(sourcemsg); Bukkit.getLogger().warning(sourcemsg);
e.printStackTrace(); e.printStackTrace();
} }
@ -131,4 +139,16 @@ public final class TBMCCoreAPI {
public static void RegisterEventsForExceptions(Listener listener, Plugin plugin) { public static void RegisterEventsForExceptions(Listener listener, Plugin plugin) {
EventExceptionHandler.registerEvents(listener, plugin, new EventExceptionCoreHandler()); EventExceptionHandler.registerEvents(listener, plugin, new EventExceptionCoreHandler());
} }
/**
* Send exceptions that haven't been sent (their events didn't get handled). This method is used by the DiscordPlugin's ready event
*/
public static void SendUnsentExceptions() {
for (Entry<String, Throwable> entry : exceptionsToSend.entrySet()) {
TBMCExceptionEvent event = new TBMCExceptionEvent(entry.getKey(), entry.getValue());
Bukkit.getPluginManager().callEvent(event);
if (event.isHandled())
exceptionsToSend.remove(entry.getKey());
}
}
} }

View file

@ -16,6 +16,7 @@ public class TBMCExceptionEvent extends Event {
private String sourcemsg; private String sourcemsg;
private Throwable exception; private Throwable exception;
private boolean handled;
TBMCExceptionEvent(String sourcemsg, Throwable exception) { TBMCExceptionEvent(String sourcemsg, Throwable exception) {
this.sourcemsg = sourcemsg; this.sourcemsg = sourcemsg;
@ -40,6 +41,22 @@ public class TBMCExceptionEvent extends Event {
return exception; return exception;
} }
/**
* Gets if this event was handled
*
* @return True if it was handled
*/
public boolean isHandled() {
return handled;
}
/**
* Flags the event as handled
*/
public void setHandled() {
this.handled = true;
}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;