Managing ZenodoException as a JSON

This commit is contained in:
Francesco Mangiacrapa 2020-01-31 16:17:50 +01:00
parent 555f022997
commit c277bc2783
2 changed files with 30 additions and 4 deletions

View File

@ -23,6 +23,10 @@ import com.github.gwtbootstrap.client.ui.constants.AlertType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
@ -148,7 +152,25 @@ public class CkanToZendoPublisherWidget {
Alert erroLabel = new Alert();
//erroLabel.setClose(false);
erroLabel.setType(AlertType.ERROR);
erroLabel.setText(caught.getLocalizedMessage());
String message = caught.getLocalizedMessage();
try {
//Trying to parse the JSON object to display only the message
JSONValue value = JSONParser.parseStrict(message);
GWT.log("value: "+value.toString());
JSONObject jsonObject = value.isObject();
if(jsonObject!=null) {
JSONArray array = (JSONArray) jsonObject.get("errors");
//GWT.log("array: "+array.toString());
JSONObject theErrorValue = (JSONObject) array.get(0);
//GWT.log("theErrorValue: "+theErrorValue.toString());
JSONValue theMessage = theErrorValue.get("message");
//GWT.log("message: "+message.toString());
message = "Error reported from Zenodo: " + theMessage.toString();
}
}catch (Exception e) {
//silent
}
erroLabel.setText(message);
showResults(Arrays.asList(erroLabel.asWidget()));
}

View File

@ -57,6 +57,7 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
PortalUtils.getCurrentToken(this.getThreadLocalRequest(), true);
}
ZenodoDeposition zenodoDepositionPreview=loadItemPreviewFromService(zenodoItem.getName());
//(EVENTUALLY) UPDATING BASIC INFO CHANGED BY FORM ON CLIENT-SIDE
//Basic info
@ -101,7 +102,7 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
LOG.debug("Waiting for completed future computation (publishing of resources on Zenodo)...");
Thread.sleep(2000);
}
zenodoDepositionPreview = future_Dep.get(180,TimeUnit.SECONDS);
zenodoDepositionPreview = future_Dep.get(600,TimeUnit.SECONDS);
}
// Get the item representation
@ -111,15 +112,18 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
zenodoDepositionPreview = client.publish(zenodoDepositionPreview, item);
return zenodoDepositionPreview.getDoi();
//throw new ZenodoException();
}catch (TimeoutException e) {
String error = "I'm waiting too time for publishing on Zenodo the catalogue item: "+zenodoItem.getName()+". Check by yourself the result later";
String error = "I'm waiting too time to upload the files to Zenodo. Check by yourself the result later";
LOG.error(error, e);
String clientError = String.format("%s", error);
throw new Exception(clientError);
} catch (ZenodoException e) {
String error = "ZenodoException during upload to Zenodo the catalogue item: "+zenodoItem.getName();
LOG.error(error, e);
String clientError = String.format("%s", e.getMessage());
String clientError = String.format("%s", e.getRemoteMessage());
clientError = "{\"status\": 400, \"message\": \"Validation error.\", \"errors\": [{\"field\": null, \"message\": \"New version's files must differ from all previous versions.\", \"code\": 10}]}";
throw new Exception(clientError);
} catch (Exception e) {
String error = "Error during upload to Zenodo the catalogue item: "+zenodoItem.getName();