Mercurial Hosting > nabble
diff src/nabble/view/lib/NabbleErrorHandler.jtp @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children | 18cf4872fd7f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/lib/NabbleErrorHandler.jtp Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,262 @@ +<% +package nabble.view.lib; + +import fschmidt.util.java.HtmlUtils; +import fschmidt.util.servlet.ServletUtils; +import nabble.model.Init; +import nabble.model.Site; +import nabble.model.UpdatingException; +import nabble.model.User; +import org.eclipse.jetty.http.HttpGenerator; +import org.eclipse.jetty.server.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; + + +public final class NabbleErrorHandler extends org.eclipse.jetty.server.handler.ErrorHandler { + + private static final Logger logger = LoggerFactory.getLogger(NabbleErrorHandler.class); + private HttpServletResponse response; + + public void handle(String string, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException { + this.response = httpServletResponse; + super.handle(string, request, httpServletRequest, httpServletResponse); + } + + protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) + throws IOException + { + if (code == HttpServletResponse.SC_SERVICE_UNAVAILABLE) { + serviceUnavailablePage(writer); + return; + } + if (message == null) + message = HttpGenerator.getReasonBuffer(code).toString(); + + Throwable throwable = (Throwable)request.getAttribute(NabbleErrorFilter.WORK_AROUND_ERROR_EXCEPTION); + if (UpdatingException.isIn(throwable)) { + logger.error("Updating database: "+ServletUtils.getCurrentURL(request)); + databaseUpdatePage(writer); + return; + } + try { + writeMyErrorPage(request,writer,code,message,showStacks, throwable); + } catch (ServletException e) { + logger.error(ServletUtils.getCurrentURL(request), e); + throw new RuntimeException(e); + } catch (RuntimeException e) { + logger.error(ServletUtils.getCurrentURL(request), e); + throw e; + } + } + + private void writeMyErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks, Throwable throwable) + throws IOException, ServletException + { + PrintWriter out = new PrintWriter(writer); + boolean hasTweaks = false; + boolean isAdmin = false; + String homeLink; + Site site = null; + try { + site = Jtp.getSite(request); + } catch(RuntimeException e) {} + if( site == null ) { + homeLink = "<a href='" + Jtp.homePage() + "'>Nabble</a>"; + } else { + homeLink = Jtp.link(site.getRootNode()); + hasTweaks = site.getCustomTweaks().size() > 0; + User user = Jtp.getUser(request, response); + isAdmin = user != null && (site.getRootNode().getOwner().equals(user) || Permissions.isInGroup(user, "Administrators")); + } + %> + <html> + <head> + <% writeErrorPageHead(request,writer,code,message); %> + <META NAME="robots" CONTENT="noindex,nofollow"> + <link rel="stylesheet" href="<%=Shared.getCssPath()%>" type="text/css" /> + <style type="text/css"> + p { margin: .3em 0 0; } + .error-details { + border: 1px solid gray; + padding: .5em; + width:90%; + margin-left:2.5%; + position: relative; + overflow-x:scroll; + } + </style> + </head> + <body> + <% + String uri = request.getRequestURI(); + if (uri!=null) { + uri = HtmlUtils.htmlEncode(uri); + } + %> + <div class="nabble" id="nabble"> + <div class="top-bar"> + <div class="breadcrumbs" style="float:left;"> + <%=homeLink%> + </div> + </div> + <% + if (throwable != null) { + if( throwable instanceof ServletException ) { + if( MinorServletException.isIn(throwable) ) { + logger.info(ServletUtils.getCurrentURL(request), throwable); + } else { + logger.warn(ServletUtils.getCurrentURL(request), throwable); + } + } else { + logger.error(ServletUtils.getCurrentURL(request), throwable); + } + %> + <h1>Oops... An error has occurred</h1> + + Please contact <a href="<%=Jtp.supportUrl()%>" target="_new">Nabble Support</a> and explain what you did to cause this error. + Your feedback is very important to us. + + <% if (hasTweaks) { %> + <% if (isAdmin) { %> + <div class="info-message rounded" style="margin-top:1em;padding:.5em .3em"> + <h3 style="margin-top:0;padding-top:0">Modified NAML Code</h3> + <%=site.getRootNode().getSubjectHtml()%> has modified NAML code and this could be the cause of the error below. + Please take a careful look at your changes and try to make sure your code is not broken.<br/> + </div> + <% } %> + <div style="margin:1.2em 0;font-weight:bold"> + <img src="/images/tool.png" width="16" height="17" style="vertical-align:-25%"/> + <a href="/template/NamlEditor.jtp">Go to NAML Editor</a> + </div> + <% } %> + + <h2 style="margin-top:1em">More Details</h2> + <div class="error-details light-bg-color"> + <h3>Error <%=code%></h3> + <pre><%=HtmlUtils.htmlEncode(message)%></pre> + <% if (throwable.getCause() != null) { + String msg = throwable.getCause().getMessage(); + if( msg != null ) { + int posBreak = msg.indexOf("\n\t"); + msg = posBreak > 0? msg.substring(0, posBreak) : msg; + msg = HtmlUtils.htmlEncode(msg); + msg = msg.replaceAll("\n", "<br/>").replaceAll("\t"," "); + %> + <p><b>Message</b>: <%=msg%></p> + <% + } + }%> + <p><b>RequestURI</b>: <%=uri%></p> + <p><b>Server</b>: <%=Jtp.getDefaultHost()%></p> + <% + + if (showStacks) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + throwable.printStackTrace(pw); + pw.flush(); + %><h3 style="margin-top:1.2em">Caused by:</h3><pre><%=HtmlUtils.htmlEncode(sw.toString())%></pre><% + } + %> + </div> + <% + } else { + %> + <h2 style="padding:0;margin:1em 0"><%=message%></h2> + + Please contact <a href="<%=Jtp.supportUrl()%>" target="_new">Nabble Support</a> if you need help. + <% + } + { + StringBuffer url = request.getRequestURL(); + String query = request.getQueryString(); + if( query != null ) + url.append( '?' ).append( query ); + %> + <p>URL = <a href="<%=url%>"><%=url%></a></p> + <% + } + %> + <table class="footer-table shaded-bg-color"> + <tr> + <td class="footer-left"> + Powered by <a href="<%=Jtp.homePage()%>" target="_top" title="Free forum and other embeddable web apps">Nabble</a> + </td> + <td class="footer-right"></td> + </tr> + </table> + </div> + </body> + </html> + <% + } + + private void serviceUnavailablePage(Writer writer) { + PrintWriter out = new PrintWriter(writer); + %> + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> + <html> + <head> + <title>Service Unavailable</title> + <META NAME="robots" CONTENT="noindex,nofollow"> + </head> + <body style="text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em"> + <img src="https://www.nabble.com/assets/images/logo_nabble_home.png"/> + <h1 style="margin-top:1.5em">This Nabble server is over capacity.</h1> + <div style="margin:0 15% 2em;text-align:center;background:#eee;padding:1em 0"> + Too many requests... Please wait a moment and try again. + </div> + + Powered by <a href="https://www.nabble.com/">Nabble</a> + </div> + </body> + </html> + <% + } + + private void databaseUpdatePage(Writer writer) { + PrintWriter out = new PrintWriter(writer); + %> + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> + <html> + <head> + <title>Updating Database</title> + <META NAME="robots" CONTENT="noindex,nofollow"> + </head> + <body style="text-align:center;font-family: Verdana,sans-serif;font-size:.84em;padding-top:2em"> + <h1 style="margin-top:3em">Updating Database</h1> + <div style="margin:0 25% 2em;text-align:center;background:#eee;padding:1em 0"> + This database is being updated. Please try again in a few seconds. + </div> + <h2>Reloading in <span id="sec">60</span> seconds</h2> + <script type="text/javascript"> + var seconds = 60; + function countDown() { + if (seconds == 0) { + window.location.reload(); + return; + } + document.getElementById('sec').innerHTML = seconds; + seconds--; + setTimeout(countDown, 1000); + }; + countDown(); + </script> + + Powered by <a href="https://www.nabble.com/">Nabble</a> + </div> + </body> + </html> + <% + } +} +%>