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",
"save-button": "Save",
"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": {
"new-id": "new item"

View File

@ -84,7 +84,10 @@ SchemaAlignmentDialog._save = function(onDone) {
self._hasUnsavedChanges = false;
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.updates.ItemUpdate;
import org.openrefine.wikidata.utils.FirstLinesExtractor;
import static org.openrefine.wikidata.commands.CommandUtilities.respondError;
import com.google.refine.browsing.Engine;
import com.google.refine.commands.Command;
@ -66,14 +67,14 @@ public class PreviewWikibaseSchemaCommand extends Command {
try {
schema = WikibaseSchema.reconstruct(jsonString);
} catch (JSONException e) {
respond(response, "error", "Wikibase schema could not be parsed.");
respondError(response, e.toString());
return;
}
} else {
schema = (WikibaseSchema) project.overlayModels.get("wikibaseSchema");
}
if (schema == null) {
respond(response, "error", "No Wikibase schema provided.");
respondError(response, "No schema provided.");
return;
}

View File

@ -34,6 +34,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.operations.SaveWikibaseSchemaOperation;
import org.openrefine.wikidata.schema.WikibaseSchema;
import static org.openrefine.wikidata.commands.CommandUtilities.respondError;
import com.google.refine.commands.Command;
import com.google.refine.model.AbstractOperation;
@ -52,7 +53,7 @@ public class SaveWikibaseSchemaCommand extends Command {
String jsonString = request.getParameter("schema");
if (jsonString == null) {
respond(response, "error", "No Wikibase schema provided.");
respondError(response, "No Wikibase schema provided.");
return;
}
@ -65,8 +66,12 @@ public class SaveWikibaseSchemaCommand extends Command {
performProcessAndRespond(request, response, project, process);
} 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) {
// This is an unexpected exception, so we log it.
respondException(response, e);
}
}

View File

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

View File

@ -51,4 +51,15 @@ public class SaveWikibaseSchemaCommandTest extends SchemaCommandTest {
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,
"{\"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.TimeValue;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
import com.google.refine.tests.RefineTest;
@ -133,6 +135,13 @@ public class WikibaseSchemaTest extends RefineTest {
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
public void testEvaluateRespectsFacets()
throws JSONException, IOException {