Error handling implemented

This commit is contained in:
Norbi Peti 2016-11-02 17:54:49 +01:00
parent 3173c62cc5
commit 8e5d371a9f
5 changed files with 161 additions and 154 deletions

View file

@ -98,7 +98,7 @@
<dependency>
<groupId>com.github.TBMCPlugins.ButtonCore</groupId>
<artifactId>Towny</artifactId>
<version>master-SNAPSHOT</version>
<version>master-v1.0-g0f7e08b-32</version>
</dependency>
</dependencies>
<organization>

View file

@ -7,7 +7,7 @@ import java.util.logging.Logger;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.discordplugin.EventExceptionDiscordSender;
import buttondevteam.lib.EventExceptionCoreHandler;
import buttondevteam.lib.EventExceptionHandler;
import buttondevteam.lib.TBMCPlayer;
@ -25,7 +25,7 @@ public class MainPlugin extends JavaPlugin {
logger = getLogger();
logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ").");
EventExceptionHandler.registerEvents(new PlayerListener(), this, new EventExceptionDiscordSender());
EventExceptionHandler.registerEvents(new PlayerListener(), this, new EventExceptionCoreHandler());
}
@Override

View file

@ -0,0 +1,13 @@
package buttondevteam.lib;
import org.bukkit.event.Event;
public class EventExceptionCoreHandler extends EventExceptionHandler {
@Override
public boolean handle(Throwable ex, Event event) {
TBMCCoreAPI.SendException("An error occured while executing " + event.getEventName() + "!", ex);
return true;
}
}

View file

@ -1,100 +1,100 @@
package buttondevteam.lib;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public final class TBMCCoreAPI {
/**
* Updates or installs the specified plugin. The plugin must use Maven.
*
* @param name
* The plugin's repository name.
* @return Error message or empty string
*/
public static String UpdatePlugin(String name) {
List<String> plugins = GetPluginNames();
String correctname = null;
for (String plugin : plugins) {
if (plugin.equalsIgnoreCase(name)) {
correctname = plugin; // Fixes capitalization
break;
}
}
if (correctname == null) {
Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name);
return "Can't find plugin: " + name;
}
Bukkit.getLogger().info("Updating TBMC plugin: " + correctname);
String ret = "";
URL url;
try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/"
+ correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar"));
} catch (FileNotFoundException e) {
ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/"
+ correctname + "/master-SNAPSHOT/build.log";
} catch (IOException e) {
ret = "IO error!\n" + e.getMessage();
} catch (Exception e) {
Bukkit.getLogger().warning("Error!\n" + e);
ret = e.toString();
}
return ret;
package buttondevteam.lib;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public final class TBMCCoreAPI {
/**
* Updates or installs the specified plugin. The plugin must use Maven.
*
* @param name
* The plugin's repository name.
* @return Error message or empty string
*/
public static String UpdatePlugin(String name) {
List<String> plugins = GetPluginNames();
String correctname = null;
for (String plugin : plugins) {
if (plugin.equalsIgnoreCase(name)) {
correctname = plugin; // Fixes capitalization
break;
}
}
if (correctname == null) {
Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name);
return "Can't find plugin: " + name;
}
Bukkit.getLogger().info("Updating TBMC plugin: " + correctname);
String ret = "";
URL url;
try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/"
+ correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar"));
} catch (FileNotFoundException e) {
ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/"
+ correctname + "/master-SNAPSHOT/build.log";
} catch (IOException e) {
ret = "IO error!\n" + e.getMessage();
} catch (Exception e) {
Bukkit.getLogger().warning("Error!\n" + e);
ret = e.toString();
}
return ret;
}
/**
* Retrieves all the repository names from the GitHub organization.
*
* @return A list of names
*/
public static List<String> GetPluginNames() {
List<String> ret = new ArrayList<>();
try {
String resp = DownloadString("https://api.github.com/orgs/TBMCPlugins/repos");
JsonArray arr = new JsonParser().parse(resp).getAsJsonArray();
for (JsonElement obj : arr) {
JsonObject jobj = obj.getAsJsonObject();
ret.add(jobj.get("name").getAsString());
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String DownloadString(String urlstr) throws MalformedURLException, IOException {
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TBMCPlugins");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
/**
* Retrieves all the repository names from the GitHub organization.
*
* @return A list of names
*/
public static List<String> GetPluginNames() {
List<String> ret = new ArrayList<>();
try {
String resp = DownloadString("https://api.github.com/orgs/TBMCPlugins/repos");
JsonArray arr = new JsonParser().parse(resp).getAsJsonArray();
for (JsonElement obj : arr) {
JsonObject jobj = obj.getAsJsonObject();
ret.add(jobj.get("name").getAsString());
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String DownloadString(String urlstr) throws MalformedURLException, IOException {
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "TBMCPlugins");
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
in.close();
return body;
}
public static void SendException(String sourcemsg, Exception e) {
public static void SendException(String sourcemsg, Throwable e) {
Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e));
Bukkit.getLogger().warning(sourcemsg);
e.printStackTrace();
}
}
}

View file

@ -1,57 +1,51 @@
package buttondevteam.lib;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import buttondevteam.lib.TBMCPlayer.InfoTarget;
/**
* <p>
* This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}.
* </p>
*
* @author Norbi
*
*/
public class TBMCExceptionEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private String sourcemsg;
private Exception exception;
TBMCExceptionEvent(String sourcemsg, Exception exception) {
this.sourcemsg = sourcemsg;
this.exception = exception;
}
/**
* Gets the source message (where did this exception occur, etc.)
*
* @return The message
*/
public String getSourceMessage() {
return sourcemsg;
}
/**
* Gets the exception
*
* @return The exception
*/
public Exception getException() {
return exception;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
package buttondevteam.lib;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
/**
* <p>
* This event gets called (ideally) each time an exception occurs in a TBMC plugin. To call it, use {@link TBMCCoreAPI#SendException(String, Exception)}.
* </p>
*
* @author Norbi
*
*/
public class TBMCExceptionEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private String sourcemsg;
private Throwable exception;
TBMCExceptionEvent(String sourcemsg, Throwable exception) {
this.sourcemsg = sourcemsg;
this.exception = exception;
}
/**
* Gets the source message (where did this exception occur, etc.)
*
* @return The message
*/
public String getSourceMessage() {
return sourcemsg;
}
/**
* Gets the exception
*
* @return The exception
*/
public Throwable getException() {
return exception;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}