mirror of https://github.com/apache/jclouds.git
better error message when sax parsing fails
This commit is contained in:
parent
4823c8b8f5
commit
fc0277523f
|
@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
import static com.google.common.io.Closeables.closeQuietly;
|
import static com.google.common.io.Closeables.closeQuietly;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
@ -33,6 +34,7 @@ import org.jclouds.rest.InvocationContext;
|
||||||
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
||||||
import org.jclouds.util.Strings2;
|
import org.jclouds.util.Strings2;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.SAXParseException;
|
import org.xml.sax.SAXParseException;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
import org.xml.sax.helpers.DefaultHandler;
|
import org.xml.sax.helpers.DefaultHandler;
|
||||||
|
@ -71,7 +73,14 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
|
||||||
}
|
}
|
||||||
if (from.getStatusCode() >= 300)
|
if (from.getStatusCode() >= 300)
|
||||||
return convertStreamToStringAndParse(from);
|
return convertStreamToStringAndParse(from);
|
||||||
return parse(from.getPayload().getInput());
|
InputStream is = from.getPayload().getInput();
|
||||||
|
try {
|
||||||
|
return parse(new InputSource(is));
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
return addDetailsAndPropagate(from, e);
|
||||||
|
} finally {
|
||||||
|
closeQuietly(is);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private T convertStreamToStringAndParse(HttpResponse from) {
|
private T convertStreamToStringAndParse(HttpResponse from) {
|
||||||
|
@ -102,15 +111,19 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
|
||||||
|
|
||||||
public T parse(InputSource from) {
|
public T parse(InputSource from) {
|
||||||
try {
|
try {
|
||||||
|
return doParse(from);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return addDetailsAndPropagate(null, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected T doParse(InputSource from) throws IOException, SAXException {
|
||||||
checkNotNull(from, "xml inputsource");
|
checkNotNull(from, "xml inputsource");
|
||||||
from.setEncoding("UTF-8");
|
from.setEncoding("UTF-8");
|
||||||
parser.setContentHandler(getHandler());
|
parser.setContentHandler(getHandler());
|
||||||
// This method should accept documents with a BOM (Byte-order mark)
|
// This method should accept documents with a BOM (Byte-order mark)
|
||||||
parser.parse(from);
|
parser.parse(from);
|
||||||
return getHandler().getResult();
|
return getHandler().getResult();
|
||||||
} catch (Exception e) {
|
|
||||||
return addDetailsAndPropagate(null, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T addDetailsAndPropagate(HttpResponse response, Exception e) {
|
public T addDetailsAndPropagate(HttpResponse response, Exception e) {
|
||||||
|
|
Loading…
Reference in New Issue