Narrow exceptions thrown and let them propagate up so we know

workspace file isn't valid - first step for #528
This commit is contained in:
Tom Morris 2013-08-05 13:08:02 -04:00
parent cc37c7053f
commit c3cab0524a
3 changed files with 10 additions and 16 deletions

View File

@ -105,7 +105,7 @@ public class ProjectMetadata implements Jsonizable {
writer.endObject();
}
public void write(JSONWriter jsonWriter) throws Exception {
public void write(JSONWriter jsonWriter) throws JSONException {
Properties options = new Properties();
options.setProperty("mode", "save");

View File

@ -231,6 +231,9 @@ public class FileProjectManager extends ProjectManager {
/**
* Save the workspace's data out to file in a safe way: save to a temporary file first
* and rename it to the real file.
* <p>
* FIXME: Even though this attempts to be safe by writing new file and renaming,
* it's still possible for it to corrupt things.
*/
@Override
protected void saveWorkspace() {
@ -273,11 +276,7 @@ public class FileProjectManager extends ProjectManager {
if (metadata != null) {
jsonWriter.value(id);
try {
ProjectMetadataUtilities.save(metadata, getProjectDir(id));
} catch (Exception e) {
e.printStackTrace();
}
ProjectMetadataUtilities.save(metadata, getProjectDir(id));
}
}
jsonWriter.endArray();

View File

@ -36,9 +36,11 @@ package com.google.refine.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter;
@ -51,16 +53,9 @@ import com.google.refine.ProjectMetadata;
public class ProjectMetadataUtilities {
final static Logger logger = LoggerFactory.getLogger("project_metadata_utilities");
public static void save(ProjectMetadata projectMeta, File projectDir) throws Exception {
public static void save(ProjectMetadata projectMeta, File projectDir) throws JSONException, IOException {
File tempFile = new File(projectDir, "metadata.temp.json");
try {
saveToFile(projectMeta, tempFile);
} catch (Exception e) {
e.printStackTrace();
logger.warn("Failed to save project metadata");
return;
}
saveToFile(projectMeta, tempFile);
File file = new File(projectDir, "metadata.json");
File oldFile = new File(projectDir, "metadata.old.json");
@ -75,7 +70,7 @@ public class ProjectMetadataUtilities {
}
}
protected static void saveToFile(ProjectMetadata projectMeta, File metadataFile) throws Exception {
protected static void saveToFile(ProjectMetadata projectMeta, File metadataFile) throws JSONException, IOException {
Writer writer = new OutputStreamWriter(new FileOutputStream(metadataFile));
try {
JSONWriter jsonWriter = new JSONWriter(writer);