Add error message when saving an incomplete schema

This commit is contained in:
Antonin Delpeuch 2018-03-18 15:53:23 +00:00
parent 1d5ffddf90
commit 8fab42ce8f
9 changed files with 71 additions and 7 deletions

View File

@ -41,7 +41,8 @@
"reset-button": "Reset", "reset-button": "Reset",
"save-button": "Save", "save-button": "Save",
"close-button": "Close", "close-button": "Close",
"unsaved-changes": "There are unsaved changes. Close anyway?" "unsaved-changes": "There are unsaved changes. Close anyway?",
"incomplete-schema-could-not-be-saved": "Your schema is incomplete so it cannot be saved yet."
}, },
"wikidata-preview": { "wikidata-preview": {
"new-id": "new item" "new-id": "new item"

View File

@ -84,7 +84,10 @@ SchemaAlignmentDialog._save = function(onDone) {
self._hasUnsavedChanges = false; self._hasUnsavedChanges = false;
if (onDone) onDone(); if (onDone) onDone();
} },
onError: function(e) {
alert($.i18n._('wikidata-schema')["incomplete-schema-could-not-be-saved"]);
},
} }
); );
}; };

View File

@ -0,0 +1,32 @@
package org.openrefine.wikidata.commands;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONWriter;
public class CommandUtilities {
/**
* Helper introduced to ease returning error messages from a response. Curiously
* this is not part of {@link Command}: the respond method uses the "status" JSON
* key instead of the "code" one required by the JS code.
*
* @param response
* @param errorMessage
* @throws IOException
*/
public static void respondError(HttpServletResponse response, String errorMessage)
throws IOException {
Writer w = response.getWriter();
JSONWriter writer = new JSONWriter(w);
writer.object();
writer.key("code"); writer.value("error");
writer.key("message"); writer.value(errorMessage);
writer.endObject();
w.flush();
w.close();
}
}

View File

@ -42,6 +42,7 @@ import org.openrefine.wikidata.qa.QAWarningStore;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.utils.FirstLinesExtractor; import org.openrefine.wikidata.utils.FirstLinesExtractor;
import static org.openrefine.wikidata.commands.CommandUtilities.respondError;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
@ -66,14 +67,14 @@ public class PreviewWikibaseSchemaCommand extends Command {
try { try {
schema = WikibaseSchema.reconstruct(jsonString); schema = WikibaseSchema.reconstruct(jsonString);
} catch (JSONException e) { } catch (JSONException e) {
respond(response, "error", "Wikibase schema could not be parsed."); respondError(response, e.toString());
return; return;
} }
} else { } else {
schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema"); schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
} }
if (schema == null) { if (schema == null) {
respond(response, "error", "No Wikibase schema provided."); respondError(response, "No schema provided.");
return; return;
} }

View File

@ -34,6 +34,7 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation; import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation;
import org.openrefine.wikidata.schema.WikibaseSchema; import org.openrefine.wikidata.schema.WikibaseSchema;
import static org.openrefine.wikidata.commands.CommandUtilities.respondError;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
@ -52,7 +53,7 @@ public class SaveWikibaseSchemaCommand extends Command {
String jsonString = request.getParameter("schema"); String jsonString = request.getParameter("schema");
if (jsonString == null) { if (jsonString == null) {
respond(response, "error", "No Wikibase schema provided."); respondError(response, "No Wikibase schema provided.");
return; return;
} }
@ -65,8 +66,12 @@ public class SaveWikibaseSchemaCommand extends Command {
performProcessAndRespond(request, response, project, process); performProcessAndRespond(request, response, project, process);
} catch (JSONException e) { } catch (JSONException e) {
respond(response, "error", "Wikibase schema could not be parsed."); // We do not use respondException here because this is an expected
// exception which happens every time a user tries to save an incomplete
// schema - the exception should not be logged.
respondError(response, "Invalid Wikibase schema provided.");
} catch (Exception e) { } catch (Exception e) {
// This is an unexpected exception, so we log it.
respondException(response, e); respondException(response, e);
} }
} }

View File

@ -186,7 +186,7 @@ public class WikibaseSchema implements OverlayModel {
return reconstruct(o.toString()); return reconstruct(o.toString());
} }
static public WikibaseSchema reconstruct(String json) { static public WikibaseSchema reconstruct(String json) throws JSONException {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
try { try {
return mapper.readValue(json, WikibaseSchema.class); return mapper.readValue(json, WikibaseSchema.class);

View File

@ -51,4 +51,15 @@ public class SaveWikibaseSchemaCommandTest extends SchemaCommandTest {
assertTrue(writer.toString().contains("\"ok\"")); assertTrue(writer.toString().contains("\"ok\""));
} }
@Test
public void testInvalidSchema() throws ServletException, IOException {
String schemaJson = "{\"itemDocuments\":[{\"statementGroups\":[{\"statements\":[]}],"
+"\"nameDescs\":[]}],\"wikibasePrefix\":\"http://www.wikidata.org/entity/\"}";
when(request.getParameter("schema")).thenReturn(schemaJson);
command.doPost(request, response);
assertTrue(writer.toString().contains("\"error\""));
}
} }

View File

@ -78,4 +78,6 @@ public class WbItemVariableTest extends WbVariableTest<ItemIdValue> {
JacksonSerializationTest.canonicalSerialization(WbExpression.class, variable, JacksonSerializationTest.canonicalSerialization(WbExpression.class, variable,
"{\"type\":\"wbitemvariable\",\"columnName\":\"column A\"}"); "{\"type\":\"wbitemvariable\",\"columnName\":\"column A\"}");
} }
// TODO: test with column reconciled against different identifier space
} }

View File

@ -52,6 +52,8 @@ import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
@ -133,6 +135,13 @@ public class WikibaseSchemaTest extends RefineTest {
assertEquals(expected, updates); assertEquals(expected, updates);
} }
@Test(expectedExceptions = JSONException.class)
public void testDeserializeEmpty() throws JSONException {
String schemaJson = "{\"itemDocuments\":[{\"statementGroups\":[{\"statements\":[]}],"
+"\"nameDescs\":[]}],\"wikibasePrefix\":\"http://www.wikidata.org/entity/\"}";
WikibaseSchema.reconstruct(schemaJson);
}
@Test @Test
public void testEvaluateRespectsFacets() public void testEvaluateRespectsFacets()
throws JSONException, IOException { throws JSONException, IOException {