Permalink
Browse files

First attempt to return something useful when a piperack pipeline fails

  • Loading branch information...
ndw committed Aug 27, 2014
1 parent dfa2026 commit 23e0845211a2151f468b2b8206d2e33f328c0bd3
Showing with 123 additions and 0 deletions.
  1. +122 −0 src/com/xmlcalabash/piperack/BaseStatus.java
  2. +1 −0 src/com/xmlcalabash/piperack/PiperackApplication.java
@@ -0,0 +1,122 @@
package com.xmlcalabash.piperack;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.util.TreeWriter;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.s9api.QName;
import net.sf.saxon.trans.XPathException;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.*;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.service.StatusService;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.TransformerException;
import java.net.URI;
/**
* Created by ndw on 8/27/14.
*/
public class BaseStatus extends StatusService {
XProcRuntime globalRuntime = null;
public BaseStatus(XProcRuntime runtime) {
globalRuntime = runtime;
}
@Override
public Representation getRepresentation(org.restlet.data.Status status, Request request, Response response) {
TreeWriter tree = new TreeWriter(globalRuntime);
tree.startDocument(URI.create("http://example.com/"));
tree.addStartElement(BaseResource.pr_error);
tree.startContent();
tree.addStartElement(BaseResource.pr_code);
tree.startContent();
tree.addText("" + status.getCode());
tree.addEndElement();
tree.addStartElement(BaseResource.pr_message);
tree.startContent();
tree.addText("Bad request: " + exceptionMessage(status.getThrowable()));
tree.addEndElement();
tree.addEndElement();
tree.endDocument();
return new StringRepresentation(tree.getResult().toString());
}
private String exceptionMessage(Throwable exception) {
StructuredQName qCode = null;
SourceLocator loc = null;
String message = "";
if (exception instanceof XPathException) {
qCode = ((XPathException) exception).getErrorCodeQName();
}
if (exception instanceof TransformerException) {
TransformerException tx = (TransformerException) exception;
if (qCode == null && tx.getException() instanceof XPathException) {
qCode = ((XPathException) tx.getException()).getErrorCodeQName();
}
if (tx.getLocator() != null) {
loc = tx.getLocator();
boolean done = false;
while (!done && loc == null) {
if (tx.getException() instanceof TransformerException) {
tx = (TransformerException) tx.getException();
loc = tx.getLocator();
} else if (exception.getCause() instanceof TransformerException) {
tx = (TransformerException) exception.getCause();
loc = tx.getLocator();
} else {
done = true;
}
}
}
}
if (exception instanceof XProcException) {
XProcException err = (XProcException) exception;
loc = err.getLocator();
if (err.getErrorCode() != null) {
QName n = err.getErrorCode();
qCode = new StructuredQName(n.getPrefix(),n.getNamespaceURI(),n.getLocalName());
}
if (err.getStep() != null) {
message = message + err.getStep() + ":";
}
}
if (loc != null) {
if (loc.getSystemId() != null && !"".equals(loc.getSystemId())) {
message = message + loc.getSystemId() + ":";
}
if (loc.getLineNumber() != -1) {
message = message + loc.getLineNumber() + ":";
}
if (loc.getColumnNumber() != -1) {
message = message + loc.getColumnNumber() + ":";
}
}
if (qCode != null) {
message = message + qCode.getDisplayName() + ":";
}
message += exception.getMessage();
Throwable cause = exception.getCause();
if (cause != null) {
message += ": " + cause.getMessage();
cause = cause.getCause();
if (cause != null) {
message += ": " + cause.getMessage();
}
}
return message;
}
}
@@ -62,6 +62,7 @@ public PiperackApplication(XProcConfiguration config, XProcRuntime runtime) thro
loadPipeline(src.uri, name, src.expires);
}
setStatusService(new BaseStatus(globalRuntime));
}
@Override

0 comments on commit 23e0845

Please sign in to comment.