Implented more descriptive InvalidFilesException.

This commit is contained in:
Winston Li 2014-12-04 20:24:13 +00:00
parent 3ff9dc6b26
commit 49028bd27b

View file

@ -4,6 +4,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
@ -12,12 +13,15 @@ import java.util.List;
public class InvalidFilesException extends SnapshotPostException {
private static final String[] DESCRIPTION_LINES = {
"You have invalid files in your WriteLatex project.",
"Check your extensions."
"You have invalid files in your WriteLatex project."
};
private List<String> descriptionLines;
public InvalidFilesException(JsonObject json) {
super(json);
descriptionLines = new LinkedList<String>();
descriptionLines.addAll(Arrays.asList(DESCRIPTION_LINES));
}
@Override
@ -27,12 +31,39 @@ public class InvalidFilesException extends SnapshotPostException {
@Override
public List<String> getDescriptionLines() {
return Arrays.asList(DESCRIPTION_LINES);
return descriptionLines;
}
@Override
public void fromJSON(JsonElement json) {
System.out.println(json);
for (JsonElement error : json.getAsJsonObject().get("errors").getAsJsonArray()) {
descriptionLines.add(describeError(error.getAsJsonObject()));
}
}
private String describeError(JsonObject jsonObject) {
return jsonObject.get("file").getAsString() + " (" + describeFile(jsonObject) + ")";
}
private String describeFile(JsonObject file) {
if (file.has("cleanFile")) {
return describeCleanFile(file.get("cleanFile").getAsString());
} else {
return describeErrorState(file.get("state").getAsString());
}
}
private String describeCleanFile(String cleanFile) {
return "rename to: " + cleanFile;
}
private String describeErrorState(String state) {
if (state.equals("disallowed")) {
return "invalid file extension";
} else {
return "error";
}
}
}