better error message when sax parsing fails

This commit is contained in:
Adrian Cole 2011-07-29 03:40:29 -07:00
parent 4823c8b8f5
commit fc0277523f
1 changed files with 20 additions and 7 deletions

View File

@ -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,17 +111,21 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
public T parse(InputSource from) { public T parse(InputSource from) {
try { try {
checkNotNull(from, "xml inputsource"); return doParse(from);
from.setEncoding("UTF-8");
parser.setContentHandler(getHandler());
// This method should accept documents with a BOM (Byte-order mark)
parser.parse(from);
return getHandler().getResult();
} catch (Exception e) { } catch (Exception e) {
return addDetailsAndPropagate(null, e); return addDetailsAndPropagate(null, e);
} }
} }
protected T doParse(InputSource from) throws IOException, SAXException {
checkNotNull(from, "xml inputsource");
from.setEncoding("UTF-8");
parser.setContentHandler(getHandler());
// This method should accept documents with a BOM (Byte-order mark)
parser.parse(from);
return getHandler().getResult();
}
public T addDetailsAndPropagate(HttpResponse response, Exception e) { public T addDetailsAndPropagate(HttpResponse response, Exception e) {
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
if (request != null) { if (request != null) {