Manged ZenodoException as ZenodoError

This commit is contained in:
Francesco Mangiacrapa 2020-02-07 11:57:43 +01:00
parent 3b5d414e31
commit 583a1dc11c
3 changed files with 141 additions and 25 deletions

View File

@ -11,6 +11,7 @@ import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.Ckan2ZenodoVi
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.FormValidator;
import org.gcube.portlets.widgets.ckan2zenodopublisher.client.view.LoaderIcon;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.CatalogueItem;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.ZenodoError;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoItem;
import com.github.gwtbootstrap.client.ui.Alert;
@ -89,7 +90,8 @@ public class CkanToZendoPublisherWidget {
Alert erroLabel = new Alert();
//erroLabel.setClose(false);
erroLabel.setType(AlertType.ERROR);
erroLabel.setText(caught.getLocalizedMessage());
String message = getErrorMessage(caught);
erroLabel.setText(message);
showResults(Arrays.asList(erroLabel.asWidget()));
}
@ -152,24 +154,7 @@ public class CkanToZendoPublisherWidget {
Alert erroLabel = new Alert();
//erroLabel.setClose(false);
erroLabel.setType(AlertType.ERROR);
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
}
String message = getErrorMessage(caught);
erroLabel.setText(message);
showResults(Arrays.asList(erroLabel.asWidget()));
}
@ -209,6 +194,52 @@ public class CkanToZendoPublisherWidget {
}
/**
* Gets the error message.
*
* @param caught the caught
* @return the error message
*/
public String getErrorMessage(Throwable caught) {
String message = caught.getLocalizedMessage();
if(caught instanceof ZenodoError) {
GWT.log("Caught is instanceof "+ZenodoError.class.getName());
try {
//Trying to parse the JSON object to display only the message
JSONValue value = JSONParser.parseStrict(((ZenodoError) caught).getRemoteMessage());
GWT.log("value: "+value.toString());
JSONObject jsonObject = value.isObject();
if(jsonObject!=null) {
JSONObject theErrorJSON = jsonObject;
if(jsonObject.isArray() != null) {
JSONArray array = (JSONArray) jsonObject.get("errors");
//GWT.log("array: "+array.toString());
theErrorJSON = (JSONObject) array.get(0);
}
JSONValue theMessage = theErrorJSON.get("message");
return "Error reported from Zenodo: " + theMessage.toString();
// //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());
// return "Error reported from Zenodo: " + theMessage.toString();
}
}catch (Exception e) {
return message;
}
}
return message;
}
/**
* Show results.
*

View File

@ -22,6 +22,7 @@ import org.gcube.portlets.widgets.ckan2zenodopublisher.client.CkanToZenodoPublis
import org.gcube.portlets.widgets.ckan2zenodopublisher.server.configuration.ZenodoFieldsDescriptionsReader;
import org.gcube.portlets.widgets.ckan2zenodopublisher.server.converter.ItemToZenodoConverter;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.CatalogueItem;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.ZenodoError;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoFile;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoItem;
import org.gcube.portlets.widgets.ckan2zenodopublisher.shared.wrapped.ZenodoMetadata;
@ -120,10 +121,13 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
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();
String error = ZenodoException.class.getName()+" during upload to Zenodo the catalogue item: "+zenodoItem.getName();
LOG.error(error, e);
String clientError = String.format("%s", e.getRemoteMessage());
throw new Exception(clientError);
//String clientError = String.format("%s", e.getRemoteMessage());
ZenodoError zenodoError = new ZenodoError(e);
zenodoError.setRemoteMessage(e.getRemoteMessage());
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
throw zenodoError;
} catch (Exception e) {
String error = "Error during upload to Zenodo the catalogue item: "+zenodoItem.getName();
LOG.error(error, e);
@ -169,10 +173,13 @@ public class CkanToZenodoPublisherServiceImpl extends RemoteServiceServlet imple
return zenodoItem;
} catch (ZenodoException e) {
String error = ZenodoException.class.getSimpleName()+" on converting the catalogue item with id: "+item.getItemId();
String error = ZenodoException.class.getName()+" on converting the catalogue item with id: "+item.getItemId();
LOG.error(error, e);
String clientError = String.format("%s. %s", error, e.getMessage());
throw new Exception(clientError);
//String clientError = String.format("%s. %s", error, e.getRemoteMessage());
ZenodoError zenodoError = new ZenodoError(e);
zenodoError.setRemoteMessage(e.getRemoteMessage());
zenodoError.setResponseHTTPCode(e.getResponseHTTPCode());
throw zenodoError;
} catch (Exception e) {
String error = "Error on converting the catalogue item with id: "+item.getItemId();

View File

@ -0,0 +1,78 @@
package org.gcube.portlets.widgets.ckan2zenodopublisher.shared;
/**
* The Class ZenodoError.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
*
* Feb 7, 2020
*/
public class ZenodoError extends Exception {
/**
*
*/
private static final long serialVersionUID = -2023412792023697928L;
public ZenodoError() {
}
public ZenodoError(String arg0) {
super(arg0);
}
public ZenodoError(String arg0, Throwable cause) {
super(arg0, cause);
}
public ZenodoError(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
super(arg0, arg1, arg2, arg3);
}
public ZenodoError(Throwable arg0) {
super(arg0);
}
private String remoteMessage = null;
private Integer responseHTTPCode = 0;
/**
* Sets the response HTTP code.
*
* @param responseHTTPCode the new response HTTP code
*/
public void setResponseHTTPCode(Integer responseHTTPCode) {
this.responseHTTPCode = responseHTTPCode;
}
/**
* Gets the response HTTP code.
*
* @return the response HTTP code
*/
public Integer getResponseHTTPCode() {
return responseHTTPCode;
}
/**
* Gets the remote message.
*
* @return the remote message
*/
public String getRemoteMessage() {
return remoteMessage;
}
/**
* Sets the remote message.
*
* @param remoteMessage the new remote message
*/
public void setRemoteMessage(String remoteMessage) {
this.remoteMessage = remoteMessage;
}
}