dev@javaserverfaces.java.net

Re: What to do when an exception is thrown during ajax rendering?

From: Kito Mann <kito.mann_at_virtua.com>
Date: Fri, 7 Feb 2014 10:05:08 -0500

+1

While we're talking about the ErrorHandler, it would be nice to get an
error page in this scenario instead of requiring the developer to handle
this via JS. I have to do this in every project I'm on, and OmniFaces has
this feature as well. I should have brought this up during the JSF 2.2
work...

On Thursday, February 6, 2014, Edward Burns <edward.burns_at_oracle.com> wrote:

> https://java.net/jira/browse/JAVASERVERFACES-3171
>
> Consider this Facelets page:
>
> exceptionDuringRender.xhtml
>
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:h="http://java.sun.com/jsf/html"
> xmlns:f="http://java.sun.com/jsf/core"
> xmlns:ui="http://java.sun.com/jsf/facelets">
> <f:view contentType="text/html"/>
> <h:head>
> <meta http-equiv="Content-Type" content="text/html;
> charset=iso-8859-1"/>
> <title>Exception During Render</title>
> </h:head>
> <h:body>
>
> <p><span id="exceptionDuringRender"/>&nbsp;</p>
>
> <script type="text/javascript">
>
>
> var handleError = function handleError(data) {
> var errorMessageString = "Error from " + data.source.id;
> var span = document.getElementById("exceptionDuringRender");
> span.innerHTML = errorMessageString;
> }
>
> jsf.ajax.addOnError(handleError);
>
>
>
> </script>
>
> <h:form prependId="false">
>
> <h:outputText id="willThrowException"
> value="#{bean.throwExceptionOnAjax}" />
> <h:commandButton id="submit" value="submit">
> <f:ajax execute="@form" render=":willThrowException" />
> </h:commandButton>
>
> </h:form>
>
> </h:body>
> </html>
>
> If #{bean.throwExceptionOnAjax} throws an exception? What should happen
> to the handleError()? It should get invoked, right?
>
> Well, it doesn't. Instead, we get an exception about nested CDATA.
>
> That's the first problem.
>
> Once we fix the nested CDATA problem, we now see that the error handler
> is not invoked because the status is not set to 500.
>
> Here are my questions to the community.
>
> Should an exception occurring during render cause an HTTP 500 error to
> be set as the status of the Ajax response?
>
> Should we allow the ajax error handler to take effect when the rendering
> contains broken CDATA rendered response text?
>
> I have a patch that makes it so both of things are true.
>
> Here is the partial Ajax response with the proposed fix in place:
>
> <?xml version='1.0' encoding='UTF-8'?>
> <partial-response id="j_id1">
> <changes>
> <update id="willThrowException">
> <![CDATA[
> <partial-response id="j_id1">
> ]]>
> </update>
> </changes>
> <error>
> <error-name>class java.lang.RuntimeException</error-name>
> <error-message>
> <![CDATA[
> Intentionally throwing exception on ajax request
> ]]>
> </error-message>
> </error>
> </partial-response>
>
> Is this ok?
>
> Ed
>
> --
> | edward.burns_at_oracle.com <javascript:;> | office: +1 407 458 0017
> | 12 Work Days Til DevNexus 2014
> | 29 Work Days Til JavaLand 2014
>


-- 
___
Kito D. Mann | @kito99 | Author, JSF in Action
Virtua, Inc. | http://www.virtua.com | JSF/Java EE training and consulting
http://www.JSFCentral.com | @jsfcentral
+1 203-998-0403
* Listen to the Enterprise Java Newscast: *http://w
<http://blogs.jsfcentral.com/JSFNewscast/>ww.enterprisejavanews.com
<http://ww.enterprisejavanews.com>*
* JSFCentral Interviews Podcast:
http://www.jsfcentral.com/resources/jsfcentralpodcasts/
* Sign up for the JSFCentral Newsletter: http://oi.vresp.com/?fid=ac048d0e17