
io.datakernel.http.DebugStacktraceRenderer Maven / Gradle / Ivy
package io.datakernel.http;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* This is a util class that provides a mean to render any exception into an {@link HttpResponse}
* with the stacktrace rendered nicely.
* It also generates link to the IntelliJ IDEA REST API (http://localhost:63342) from the stacktrace,
* just like IDEA does in its log console.
*/
public final class DebugStacktraceRenderer {
private static final String IDEA_REST_API_URL = "http://localhost:63342";
private static final String DEBUG_SERVER_ERROR_HTML = "" +
"" +
"" +
"" +
"Internal Server Error " +
"" +
"" +
"" +
"" +
"" +
"Internal Server Error
" +
"
" +
"{stacktrace}
" +
"" +
"
" +
"DataKernel 3.0.0
" +
"" +
"" +
"" +
"";
private static final Pattern STACK_TRACE_ELEMENT;
static {
String ident = "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*";
STACK_TRACE_ELEMENT = Pattern.compile("(at ((?:" + ident + "\\.)+)" + ident + "\\()(" + ident + "(\\." + ident + ")(:\\d+)?)\\)");
}
public static HttpResponse render(Throwable e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
Matcher matcher = STACK_TRACE_ELEMENT.matcher(writer.toString());
StringBuffer stacktrace = new StringBuffer();
while (matcher.find()) {
String cls = matcher.group(2);
String quotedFile = Matcher.quoteReplacement(cls.substring(0, cls.length() - 1).replace('.', '/').replaceAll("\\$.*(?:\\.|$)", ""));
matcher.appendReplacement(stacktrace, "$1$3)");
}
matcher.appendTail(stacktrace);
return HttpResponse.ofCode(500)
.withHtml(DEBUG_SERVER_ERROR_HTML.replace("{stacktrace}", stacktrace));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy