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(); writer.endObject();
} }
public void write(JSONWriter jsonWriter) throws Exception { public void write(JSONWriter jsonWriter) throws JSONException {
Properties options = new Properties(); Properties options = new Properties();
options.setProperty("mode", "save"); 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 * 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. * 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 @Override
protected void saveWorkspace() { protected void saveWorkspace() {
@ -273,11 +276,7 @@ public class FileProjectManager extends ProjectManager {
if (metadata != null) { if (metadata != null) {
jsonWriter.value(id); jsonWriter.value(id);
try { ProjectMetadataUtilities.save(metadata, getProjectDir(id));
ProjectMetadataUtilities.save(metadata, getProjectDir(id));
} catch (Exception e) {
e.printStackTrace();
}
} }
} }
jsonWriter.endArray(); jsonWriter.endArray();

View File

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