Skip to content
Snippets Groups Projects
Commit befe68da authored by Thorsten Vitt's avatar Thorsten Vitt
Browse files

improved LaTeX failed message

parent 8a42633d
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.Response.StatusType;
import javax.ws.rs.ext.ExceptionMapper;
import org.springframework.web.util.HtmlUtils;
......@@ -36,24 +37,24 @@ public class GenericExceptionMapper implements ExceptionMapper<Exception> {
private static final String HTML_TEMPLATE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE html>\n"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ " <head>\n"
+ " <title>{0} {1}</title>\n"
+ " <style type=\"text/css\">'\n"
+ " h1 { border-bottom: 1px solid red; }\n"
+ " .details { color: gray; }\n"
+ " '</style>\n"
+ " </head>\n"
+ " <body>\n"
+ " <h1>{1}</h1>\n"
+ " <p class=\"message\">{2}</p>\n"
+ " <div class=\"details\">\n"
+ " <pre>\n"
+ "{3}\n"
+ " </pre>\n"
+ " </div>\n"
+ " </body>\n" + "</html>";
"<!DOCTYPE html>\n"
+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+ " <head>\n"
+ " <title>{0} {1}</title>\n"
+ " <style type=\"text/css\">'\n"
+ " h1 { border-bottom: 1px solid red; }\n"
+ " .details { color: gray; }\n"
+ " '</style>\n"
+ " </head>\n"
+ " <body>\n"
+ " <h1>{1}</h1>\n"
+ " <p class=\"message\">{2}</p>\n"
+ " <div class=\"details\">\n"
+ " <pre>\n"
+ "{3}\n"
+ " </pre>\n"
+ " </div>\n"
+ " </body>\n" + "</html>";
@Override
public Response toResponse(final Exception exception) {
......@@ -64,8 +65,8 @@ public Response toResponse(final Exception exception) {
status = STATUS_MAP.get(exception.getCause().getClass());
if (status == null)
status = Status
.fromStatusCode(((WebApplicationException) exception)
.getResponse().getStatus());
.fromStatusCode(((WebApplicationException) exception)
.getResponse().getStatus());
message = exception.getCause().getLocalizedMessage();
} else {
status = STATUS_MAP.get(exception.getClass());
......@@ -73,20 +74,31 @@ public Response toResponse(final Exception exception) {
}
if (status == null)
status = Status.INTERNAL_SERVER_ERROR;
ResponseBuilder builder = Response.status(status);
builder.type(MediaType.APPLICATION_XHTML_XML_TYPE);
builder.entity(MessageFormat.format(HTML_TEMPLATE, status
.getStatusCode(), status.getReasonPhrase(), HtmlUtils
.htmlEscapeHex(message), HtmlUtils
.htmlEscapeHex(Throwables.getStackTraceAsString(exception))));
logger.log(Level.WARNING, MessageFormat.format(
"{2} {3} returned: caught {0}: {1}", exception.getClass()
.getSimpleName(), exception.getMessage(), status
.getStatusCode(), status.getReasonPhrase()), exception);
.getSimpleName(), exception.getMessage(), status
.getStatusCode(), status.getReasonPhrase()), exception);
return builder.build();
final Response response = toResponse(status, message, Throwables.getStackTraceAsString(exception));
return response;
}
/**
* Returns a fancy HTML-formatted response for the given parameters.
* @param status the status for which the response is to be generated.
* @param message an additional informative message. Will be HTML-escaped and displayed below the status msg
* @param detail a detail message, e.g., a stack trace. Will be HTML-escaped and displayed in a preformatted way below the status msg.
* @return the generated response.
*/
public static Response toResponse(final StatusType status, final String message, final String detail) {
final ResponseBuilder builder = Response.status(status);
builder.type(MediaType.APPLICATION_XHTML_XML_TYPE);
builder.entity(MessageFormat.format(HTML_TEMPLATE, status
.getStatusCode(), status.getReasonPhrase(), HtmlUtils
.htmlEscapeHex(message), HtmlUtils
.htmlEscapeHex(detail)));
return builder.build();
}
......
package info.textgrid.services.aggregator.pdf;
import info.textgrid._import.RewriteMethod;
import info.textgrid.services.aggregator.GenericExceptionMapper;
import info.textgrid.services.aggregator.ITextGridRep;
import info.textgrid.services.aggregator.ITextGridRep.TGOSupplier;
import info.textgrid.services.aggregator.RESTUtils;
......@@ -14,6 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Set;
import java.util.logging.Logger;
......@@ -23,7 +25,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.xml.stream.XMLStreamException;
......@@ -138,10 +139,8 @@ else if (format.equals("application/pdf"))
if (latexProcess.waitFor() != 0) {
final File logFile = new File(workingDir, "data.log");
if (logFile.canRead())
return Response.status(Status.BAD_REQUEST)
.type(MediaType.TEXT_PLAIN)
.entity(logFile)
.build();
return GenericExceptionMapper.toResponse(Status.INTERNAL_SERVER_ERROR, "The requested document, " + uri + ", was transformed to a file LaTeX failed to deal with. Below is the LaTeX log file.",
Files.toString(logFile, Charset.forName("UTF-8")));
else
throw new IllegalStateException("LaTeX process failed, no log file found.");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment