refactored exception handling

This commit is contained in:
Adrian Cole 2010-06-22 15:23:44 -07:00
parent d6e06d5272
commit c79636d7b8
1 changed files with 24 additions and 13 deletions

View File

@ -35,15 +35,17 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
/** /**
* This object will parse the body of an HttpResponse and return the result of type <T> back to the * This object will parse the body of an HttpResponse and return the result of
* caller. * type <T> back to the caller.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext { public class ParseSax<T> implements Function<HttpResponse, T>,
InvocationContext {
private final XMLReader parser; private final XMLReader parser;
private final HandlerWithResult<T> handler; private final HandlerWithResult<T> handler;
@ -74,13 +76,19 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
parser.parse(new InputSource(from)); parser.parse(new InputSource(from));
return getHandler().getResult(); return getHandler().getResult();
} catch (Exception e) { } catch (Exception e) {
StringBuilder message = new StringBuilder(); if (request != null) {
if (request != null)
message.append("Error parsing input for ").append(request.getRequestLine()) StringBuilder message = new StringBuilder();
.append(": "); message.append("Error parsing input for ").append(
message.append(e.getMessage()); request.getRequestLine()).append(": ");
logger.error(e, message.toString()); message.append(e.getMessage());
throw new HttpException(message.toString(), e); logger.error(e, message.toString());
throw new HttpException(message.toString(), e);
} else {
Throwables.propagate(e);
assert false : "should have propagated: " + e;
return null;
}
} finally { } finally {
Closeables.closeQuietly(from); Closeables.closeQuietly(from);
} }
@ -91,14 +99,17 @@ public class ParseSax<T> implements Function<HttpResponse, T>, InvocationContext
} }
/** /**
* Handler that produces a useable domain object accessible after parsing completes. * Handler that produces a useable domain object accessible after parsing
* completes.
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public abstract static class HandlerWithResult<T> extends DefaultHandler implements public abstract static class HandlerWithResult<T> extends DefaultHandler
InvocationContext { implements InvocationContext {
protected GeneratedHttpRequest<?> request; protected GeneratedHttpRequest<?> request;
public abstract T getResult(); public abstract T getResult();
@Override @Override
public void setContext(GeneratedHttpRequest<?> request) { public void setContext(GeneratedHttpRequest<?> request) {
this.request = request; this.request = request;