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> <dependency>
<groupId>com.github.TBMCPlugins.ButtonCore</groupId> <groupId>com.github.TBMCPlugins.ButtonCore</groupId>
<artifactId>Towny</artifactId> <artifactId>Towny</artifactId>
<version>master-SNAPSHOT</version> <version>master-v1.0-g0f7e08b-32</version>
</dependency> </dependency>
</dependencies> </dependencies>
<organization> <organization>

View file

@ -7,7 +7,7 @@ import java.util.logging.Logger;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.discordplugin.EventExceptionDiscordSender; import buttondevteam.lib.EventExceptionCoreHandler;
import buttondevteam.lib.EventExceptionHandler; import buttondevteam.lib.EventExceptionHandler;
import buttondevteam.lib.TBMCPlayer; import buttondevteam.lib.TBMCPlayer;
@ -25,7 +25,7 @@ public class MainPlugin extends JavaPlugin {
logger = getLogger(); logger = getLogger();
logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); 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 @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; package buttondevteam.lib;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; 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;
public final class TBMCCoreAPI { public final class TBMCCoreAPI {
/** /**
* Updates or installs the specified plugin. The plugin must use Maven. * Updates or installs the specified plugin. The plugin must use Maven.
* *
* @param name * @param name
* The plugin's repository name. * The plugin's repository name.
* @return Error message or empty string * @return Error message or empty string
*/ */
public static String UpdatePlugin(String name) { public static String UpdatePlugin(String name) {
List<String> plugins = GetPluginNames(); List<String> plugins = GetPluginNames();
String correctname = null; String correctname = null;
for (String plugin : plugins) { for (String plugin : plugins) {
if (plugin.equalsIgnoreCase(name)) { if (plugin.equalsIgnoreCase(name)) {
correctname = plugin; // Fixes capitalization correctname = plugin; // Fixes capitalization
break; break;
} }
} }
if (correctname == null) { if (correctname == null) {
Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name); Bukkit.getLogger().warning("There was an error while updating TBMC plugin: " + name);
return "Can't find plugin: " + name; return "Can't find plugin: " + name;
} }
Bukkit.getLogger().info("Updating TBMC plugin: " + correctname); Bukkit.getLogger().info("Updating TBMC plugin: " + correctname);
String ret = ""; String ret = "";
URL url; URL url;
try { try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/" url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/" + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/master-SNAPSHOT/"
+ correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well + correctname + "-master-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar")); FileUtils.copyURLToFile(url, new File("plugins/" + correctname + ".jar"));
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
ret = "Can't find JAR, the build probably failed. Build log (scroll to bottom):\nhttps://jitpack.io/com/github/TBMCPlugins/" 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"; + correctname + "/master-SNAPSHOT/build.log";
} catch (IOException e) { } catch (IOException e) {
ret = "IO error!\n" + e.getMessage(); ret = "IO error!\n" + e.getMessage();
} catch (Exception e) { } catch (Exception e) {
Bukkit.getLogger().warning("Error!\n" + e); Bukkit.getLogger().warning("Error!\n" + e);
ret = e.toString(); ret = e.toString();
} }
return ret; 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;
} }
/** public static void SendException(String sourcemsg, Throwable e) {
* 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) {
Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e)); Bukkit.getPluginManager().callEvent(new TBMCExceptionEvent(sourcemsg, e));
Bukkit.getLogger().warning(sourcemsg); Bukkit.getLogger().warning(sourcemsg);
e.printStackTrace(); e.printStackTrace();
} }
} }

View file

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