Slightly better "invalid plugin.yml" errors

This commit is contained in:
Dinnerbone 2011-01-05 22:40:45 +00:00
parent d2d468b374
commit 9b227081f5
2 changed files with 92 additions and 46 deletions

View File

@ -7,6 +7,7 @@ package org.bukkit.plugin;
public class InvalidDescriptionException extends Exception { public class InvalidDescriptionException extends Exception {
private static final long serialVersionUID = 5721389122281775894L; private static final long serialVersionUID = 5721389122281775894L;
private final Throwable cause; private final Throwable cause;
private final String message;
/** /**
* Constructs a new InvalidDescriptionException based on the given Exception * Constructs a new InvalidDescriptionException based on the given Exception
@ -14,14 +15,34 @@ public class InvalidDescriptionException extends Exception {
* @param throwable Exception that triggered this Exception * @param throwable Exception that triggered this Exception
*/ */
public InvalidDescriptionException(Throwable throwable) { public InvalidDescriptionException(Throwable throwable) {
cause = throwable; this(throwable, "Invalid plugin.yml");
}
/**
* Constructs a new InvalidDescriptionException with the given message
*
* @param message Brief message explaining the cause of the exception
*/
public InvalidDescriptionException(final String message) {
this(null, message);
}
/**
* Constructs a new InvalidDescriptionException based on the given Exception
*
* @param message Brief message explaining the cause of the exception
* @param throwable Exception that triggered this Exception
*/
public InvalidDescriptionException(final Throwable throwable, final String message) {
this.cause = null;
this.message = message;
} }
/** /**
* Constructs a new InvalidDescriptionException * Constructs a new InvalidDescriptionException
*/ */
public InvalidDescriptionException() { public InvalidDescriptionException() {
cause = null; this(null, "Invalid plugin.yml");
} }
/** /**
@ -33,4 +54,9 @@ public class InvalidDescriptionException extends Exception {
public Throwable getCause() { public Throwable getCause() {
return cause; return cause;
} }
@Override
public String getMessage() {
return super.getMessage();
}
} }

View File

@ -20,11 +20,7 @@ public final class PluginDescriptionFile {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException { public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
try { loadMap((Map<String, Object>)yaml.load(stream));
loadMap((Map<String, Object>)yaml.load(stream));
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex);
}
} }
/** /**
@ -32,7 +28,7 @@ public final class PluginDescriptionFile {
* @param reader * @param reader
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public PluginDescriptionFile(final Reader reader) { public PluginDescriptionFile(final Reader reader) throws InvalidDescriptionException {
loadMap((Map<String, Object>)yaml.load(reader)); loadMap((Map<String, Object>)yaml.load(reader));
} }
@ -84,10 +80,34 @@ public final class PluginDescriptionFile {
return main; return main;
} }
private void loadMap(Map<String, Object> map) throws ClassCastException { private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
name = map.get("name").toString(); if (name == null) {
main = map.get("main").toString(); throw new InvalidDescriptionException("Name is not defined");
version = map.get("version").toString(); }
try {
name = map.get("name").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "name is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "name is of wrong type");
}
try {
version = map.get("version").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "version is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "version is of wrong type");
}
try {
main = map.get("main").toString();
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "main is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "main is of wrong type");
}
} }
private Map<String, Object> saveMap() { private Map<String, Object> saveMap() {