Skip to content

Commit

Permalink
Error page and not found page now looks the same in prod mode
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored and bschuhmann committed Nov 16, 2024
1 parent f00832c commit ccb878e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public class TemplateHtmlBuilder {
" }\n" +
"</script>\n";

private static final String HTML_TEMPLATE_START_NO_STACK = "" +
"<!doctype html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <title>%1$s%2$s</title>\n" +
" <meta charset=\"utf-8\">\n" +
"</head>";

private static final String HTML_TEMPLATE_START = "" +
"<!doctype html>\n" +
"<html lang=\"en\">\n" +
Expand Down Expand Up @@ -122,6 +130,10 @@ public class TemplateHtmlBuilder {
"</header>\n" +
"<div class=\"container content\">\n";

private static final String HEADER_TEMPLATE_NO_STACK = "<h1>%1$s</h1>\n" +
"%2$s \n" +
"<div class=\"container content\">\n";

private static final String RESOURCES_START = "<div class=\"intro\">%1$s</div><div class=\"resources\">";

private static final String ANCHOR_TEMPLATE_ABSOLUTE = "<a href=\"%1$s\">%2$s</a>";
Expand Down Expand Up @@ -185,37 +197,53 @@ public class TemplateHtmlBuilder {
private String baseUrl;

public TemplateHtmlBuilder(String title, String subTitle, String details) {
this(null, title, subTitle, details, Collections.emptyList(), null, Collections.emptyList());
this(true, null, title, subTitle, details, Collections.emptyList(), null, Collections.emptyList());
}

public TemplateHtmlBuilder(String title, String subTitle, String details, List<ErrorPageAction> actions) {
this(null, title, subTitle, details, actions, null, Collections.emptyList());
public TemplateHtmlBuilder(boolean showStack, String title, String subTitle, String details,
List<ErrorPageAction> actions) {
this(showStack, null, title, subTitle, details, actions, null, Collections.emptyList());
}

public TemplateHtmlBuilder(String baseUrl, String title, String subTitle, String details, List<ErrorPageAction> actions) {
this(baseUrl, title, subTitle, details, actions, null, Collections.emptyList());
public TemplateHtmlBuilder(String title, String subTitle, String details,
List<ErrorPageAction> actions) {
this(true, null, title, subTitle, details, actions, null, Collections.emptyList());
}

public TemplateHtmlBuilder(String title, String subTitle, String details, List<ErrorPageAction> actions, String redirect,
public TemplateHtmlBuilder(String baseUrl, String title, String subTitle, String details,
List<ErrorPageAction> actions) {
this(true, baseUrl, title, subTitle, details, actions, null, Collections.emptyList());
}

public TemplateHtmlBuilder(String title, String subTitle, String details, List<ErrorPageAction> actions,
String redirect,
List<CurrentConfig> config) {
this(null, title, subTitle, details, actions, null, Collections.emptyList());
this(true, null, title, subTitle, details, actions, null, Collections.emptyList());
}

public TemplateHtmlBuilder(String baseUrl, String title, String subTitle, String details, List<ErrorPageAction> actions,
public TemplateHtmlBuilder(boolean showStack, String baseUrl, String title, String subTitle, String details,
List<ErrorPageAction> actions,
String redirect,
List<CurrentConfig> config) {
this.baseUrl = baseUrl;
StringBuilder actionLinks = new StringBuilder();

loadCssFile();
if (showStack) {
loadCssFile();
for (ErrorPageAction epa : actions) {
actionLinks.append(buildLink(epa.name(), epa.url()));
}

StringBuilder actionLinks = new StringBuilder();
for (ErrorPageAction epa : actions) {
actionLinks.append(buildLink(epa.name(), epa.url()));
result = new StringBuilder(String.format(HTML_TEMPLATE_START, escapeHtml(title),
subTitle == null || subTitle.isEmpty() ? "" : " - " + escapeHtml(subTitle), CSS));
result.append(String.format(HEADER_TEMPLATE, escapeHtml(title), escapeHtml(details), actionLinks.toString()));
} else {
result = new StringBuilder(String.format(HTML_TEMPLATE_START_NO_STACK, escapeHtml(title),
subTitle == null || subTitle.isEmpty() ? "" : " - " + escapeHtml(subTitle), CSS));
result.append(
String.format(HEADER_TEMPLATE_NO_STACK, escapeHtml(title), escapeHtml(details), actionLinks.toString()));
}

result = new StringBuilder(String.format(HTML_TEMPLATE_START, escapeHtml(title),
subTitle == null || subTitle.isEmpty() ? "" : " - " + escapeHtml(subTitle), CSS));
result.append(String.format(HEADER_TEMPLATE, escapeHtml(title), escapeHtml(details), actionLinks.toString()));
if (!config.isEmpty()) {
result.append(String.format(CONFIG_EDITOR_HEAD, redirect));
for (CurrentConfig i : config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private void jsonResponse(RoutingContext event, String contentType, String detai

private void htmlResponse(RoutingContext event, String details, Throwable exception) {
event.response().headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8");
final TemplateHtmlBuilder htmlBuilder = new TemplateHtmlBuilder("Internal Server Error", details, details,
final TemplateHtmlBuilder htmlBuilder = new TemplateHtmlBuilder(showStack, "Internal Server Error", details, details,
this.actions);

if (decorateStack && exception != null) {
Expand Down

0 comments on commit ccb878e

Please sign in to comment.