1

我想尽可能优雅地处理我的 webapp 中的错误。
当然,我有RTFM并将以下内容添加到我的web.xml文件中:

<!-- WEB-INF/web.xml -->
<error-page>
    <exception-type>java.lang.Throwable</exception-type>    
    <location>/error.zul</location>    
</error-page>

这是基本的Java webapp 错误处理,效果很好。由于 ZK 是一个 AJAXy 框架,因此在呈现页面后可能会引发错误,因此 ZK 也为此提供了自己的错误处理:

<!-- zk.xml -->
<error-page>
    <exception-type>java.lang.Throwable</exception-type>    
    <location>/zk/common/error.zul</location>    
</error-page>

这也很好用,但是,它们玩得不好。当在 AU 请求期间引发错误时,ZK 错误消息会按原样显示,但我得到以下信息stderr

Feb 26, 2013 10:37:04 PM org.zkoss.zk.ui.http.DHtmlLayoutServlet handleError:273
WARNING: Failed to load the error page: /zk/common/error.zul
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:168)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:267)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet auEngine threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet zkLoader threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)
Feb 26, 2013 10:37:04 PM org.apache.catalina.core.StandardHostValve custom
SEVERE: Exception Processing ErrorPage[exceptionType=java.lang.Throwable, location=/error.zul]
java.lang.IllegalStateException: getOutputStream() has already been called for this response
    at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112)
    at org.zkoss.web.servlet.dsp.ServletDspContext.getOut(ServletDspContext.java:113)
    at org.zkoss.web.servlet.dsp.impl.TextNode.interpret(TextNode.java:44)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:51)
    at org.zkoss.web.servlet.dsp.impl.RootNode.interpret(RootNode.java:60)
    at org.zkoss.web.util.resource.DspExtendlet.service(DspExtendlet.java:92)
    at org.zkoss.web.util.resource.ClassWebResource.web0(ClassWebResource.java:583)
    at org.zkoss.web.util.resource.ClassWebResource.web(ClassWebResource.java:569)
    at org.zkoss.web.util.resource.ClassWebResource.service(ClassWebResource.java:260)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doGet(DHtmlUpdateServlet.java:392)
    at org.zkoss.zk.au.http.DHtmlUpdateServlet.doPost(DHtmlUpdateServlet.java:464)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.zkoss.web.servlet.Servlets.include(Servlets.java:882)
    at org.zkoss.zk.ui.http.Utils.handleError(Utils.java:143)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.handleError(DHtmlLayoutServlet.java:278)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:138)
    at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doPost(DHtmlLayoutServlet.java:149)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488)
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:416)
    at org.apache.catalina.core.StandardHostValve.throwable(StandardHostValve.java:270)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:680)

同样,这是 ZK 特定错误处理程序 (at /zk/common/error.zul) 拦截错误的时候。根据堆栈跟踪,似乎错误会传播到正常的 Java webapp 错误处理程序 (at /error.zul)。

如果第一次呈现页面时发生错误,则显示正常的错误处理程序(位于`/error.zul')而不是按预期显示页面,不会记录任何问题。

关于如何解决这个问题的任何想法?我可以/应该在我的 ZK 错误处理程序上做些什么来阻止错误传播吗?

4

1 回答 1

0

您可以使用 Initiator 来处理 ZK Loader 呈现页面时发生的异常。使用的标签是

<?init class="com.foo.MyInitial"?>

您需要实现Initiator

希望这可以帮助。

参考:链接

于 2013-02-27T18:49:25.033 回答