Add more detail error messages when parsing regions file.

This commit is contained in:
sk89q 2014-07-11 13:20:42 -07:00
parent b0ed119585
commit 0d2adc5707

View File

@ -19,17 +19,6 @@
package com.sk89q.worldguard.protection.databases; package com.sk89q.worldguard.protection.databases;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import com.sk89q.util.yaml.YAMLFormat; import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLNode; import com.sk89q.util.yaml.YAMLNode;
import com.sk89q.util.yaml.YAMLProcessor; import com.sk89q.util.yaml.YAMLProcessor;
@ -44,9 +33,31 @@
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException; import com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.representer.Representer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
public class YAMLDatabase extends AbstractProtectionDatabase { public class YAMLDatabase extends AbstractProtectionDatabase {
/**
* Used to dump YAML when an error occurs
*/
private static Yaml yaml;
private YAMLProcessor config; private YAMLProcessor config;
private Map<String, ProtectedRegion> regions; private Map<String, ProtectedRegion> regions;
private final Logger logger; private final Logger logger;
@ -78,21 +89,20 @@ public void load() throws ProtectionDatabaseException {
return; return;
} }
Map<String,ProtectedRegion> regions = Map<String,ProtectedRegion> regions = new HashMap<String,ProtectedRegion>();
new HashMap<String,ProtectedRegion>(); Map<ProtectedRegion,String> parentSets = new LinkedHashMap<ProtectedRegion, String>();
Map<ProtectedRegion,String> parentSets =
new LinkedHashMap<ProtectedRegion, String>();
for (Map.Entry<String, YAMLNode> entry : regionData.entrySet()) { for (Map.Entry<String, YAMLNode> entry : regionData.entrySet()) {
String id = entry.getKey().toLowerCase().replace(".", ""); String id = entry.getKey().toLowerCase().replace(".", "");
YAMLNode node = entry.getValue(); YAMLNode node = entry.getValue();
String type = node.getString("type"); String type = node.getString("type");
ProtectedRegion region; ProtectedRegion region;
try { try {
if (type == null) { if (type == null) {
logger.warning("Undefined region type for region '" + id + '"'); logger.warning("Undefined region type for region '" + id + "'!\n" +
"Here is what the region data looks like:\n\n" + dumpAsYaml(entry.getValue().getMap()) + "\n");
continue; continue;
} else if (type.equals("cuboid")) { } else if (type.equals("cuboid")) {
Vector pt1 = checkNonNull(node.getVector("min")); Vector pt1 = checkNonNull(node.getVector("min"));
@ -108,7 +118,8 @@ public void load() throws ProtectionDatabaseException {
} else if (type.equals("global")) { } else if (type.equals("global")) {
region = new GlobalProtectedRegion(id); region = new GlobalProtectedRegion(id);
} else { } else {
logger.warning("Unknown region type for region '" + id + '"'); logger.warning("Unknown region type for region '" + id + "'!\n" +
"Here is what the region data looks like:\n\n" + dumpAsYaml(entry.getValue().getMap()) + "\n");
continue; continue;
} }
@ -124,7 +135,10 @@ public void load() throws ProtectionDatabaseException {
parentSets.put(region, parentId); parentSets.put(region, parentId);
} }
} catch (NullPointerException e) { } catch (NullPointerException e) {
logger.warning("Missing data for region '" + id + '"'); logger.log(Level.WARNING,
"Unexpected NullPointerException encountered during parsing for the region '" + id + "'!\n" +
"Here is what the region data looks like:\n\n" + dumpAsYaml(entry.getValue().getMap()) +
"\n\nNote: This region will disappear as a result!", e);
} }
} }
@ -135,8 +149,7 @@ public void load() throws ProtectionDatabaseException {
try { try {
entry.getKey().setParent(parent); entry.getKey().setParent(parent);
} catch (CircularInheritanceException e) { } catch (CircularInheritanceException e) {
logger.warning("Circular inheritance detect with '" logger.warning("Circular inheritance detect with '" + entry.getValue() + "' detected as a parent");
+ entry.getValue() + "' detected as a parent");
} }
} else { } else {
logger.warning("Unknown region parent: " + entry.getValue()); logger.warning("Unknown region parent: " + entry.getValue());
@ -311,5 +324,27 @@ public Map<String, ProtectedRegion> getRegions() {
public void setRegions(Map<String, ProtectedRegion> regions) { public void setRegions(Map<String, ProtectedRegion> regions) {
this.regions = regions; this.regions = regions;
} }
/**
* Dump the given object as YAML for debugging purposes.
*
* @param object the object
* @return the YAML string or an error string if dumping fals
*/
private static String dumpAsYaml(Object object) {
if (yaml == null) {
DumperOptions options = new DumperOptions();
options.setIndent(4);
options.setDefaultFlowStyle(FlowStyle.AUTO);
yaml = new Yaml(new SafeConstructor(), new Representer(), options);
}
try {
return yaml.dump(object).replaceAll("(?m)^", "\t");
} catch (Throwable t) {
return "<error while dumping object>";
}
}
} }