Skip to main content

What is the recommended way to catch exceptions and display an error page?

8 replies [Last post]
hirowla
Offline
Joined: 2009-04-22

Just wondering what the recommended way is to catch any uncaught exceptions (usually application errors, but maybe Faces ones as well) and direct the user to a custom error page, one which I can control the content of?

I'm using 2.0.2. I know things have changed a lot since 1.x but can't find examples for 2.0. I've tried using the error page inside web.xml, but it never gets hit. I've tried writing a filter on the FacesServlet, but the exception never seems to bubble up. I can see ExceptionHandler and ExceptionQueueEvent but can't think of a way to tie it all together.

Quite simply I'd like an error page (preferably a Faces page) to display part of the error for me, and display a code (which I'll be logging so it can be cross-referenced later).

Any ideas?

Thanks,

Ian

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hirowla
Offline
Joined: 2009-04-22

After further research, I've answered my own question.

I found this article: http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-graceful... . Doesn't exactly solve what I am trying to do, but the hooks allow me to do it. Instead of inspecting only certain exceptions and passing the rest on, I catch all exceptions, pass them onto my error page and then swallow them. This means that I can redirect to a JSF error page.

The code's a bit ugly, but I only have to write it once per project.

I also read somewhere that this area is something being considered for a future release of JSF2 (probably 2.1 I guess).

hirowla
Offline
Joined: 2009-04-22

Any ideas? I'm just not sure how to tie it all together and work as expected, as it seems very different for JSF2 (and all the examples are for JSF1).

Shing Wai Chan

One can specify a default error page in web.xml
as follows:


/defaultErrorPage.jsp

Regards,
Shing Wai Chan

webtier@javadesktop.org wrote:
> Any ideas? I'm just not sure how to tie it all together and work as expected, as it seems very different for JSF2 (and all the examples are for JSF1).
> [Message sent by forum member 'hirowla' (ian.rowlands@three.com.au)]
>
> http://forums.java.net/jive/thread.jspa?messageID=384386
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

hirowla
Offline
Joined: 2009-04-22

Thanks for the suggestion, but I have tried that. I think the Faces servlet is not passing it back up. I know it does things with the exception, but I don't know how to access it.

Lincoln Baxter, III

I would suggest writing a Servlet Filter that simple continues onward in the
chain, catches all exceptions, and wraps them in one known exception,
re-throwing that known exception.

Once that is done, you can map that wrapper exception to the Error page,
which should do what you need - sending any Exception to the error page:

http://download-llnw.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/web_...

--
Lincoln Baxter, III
http://ocpsoft.com
http://scrumshark.com
"Keep it Simple"

On Tue, Feb 2, 2010 at 8:54 PM, wrote:

> Thanks for the suggestion, but I have tried that. I think the Faces servlet
> is not passing it back up. I know it does things with the exception, but I
> don't know how to access it.
> [Message sent by forum member 'hirowla' (ian.rowlands@three.com.au)]
>
> http://forums.java.net/jive/thread.jspa?messageID=384390
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>
[att1.html]

hirowla
Offline
Joined: 2009-04-22

Ok, I found out one of my problems - if I set the project stage to Development, my exception gets swallowed. So changing it to Production at least allows a filter to catch it.

The main reason for my question is that with JSF2 there is so much error handling/system event stuff in there that it seems a "hack" to do it via a filter. Obviously it can be processed and an error page sent back - heck, when you set the stage to "Development" a custom error page comes up. However it's not my error page, which is the issue.

Has anybody tried just using the stuff already in JSF2 to do this, or knows how the RI does it so I can do something similar? It just seems a shame to keep using the same hacks used in the past when I'm sure I'm just missing something simple or obvious or both! These error handling classes were pushed as one of the great new features of JSF2, I just need to use how to use them properly!

Thanks,

Ian

Lincoln Baxter, III

I've found that the Filter solution is the easiest and least hacky. I
wouldn't bother with the JSF Exception handler if you can avoid it. It's a
lot more work than a 5 line filter :)

On Wed, Feb 3, 2010 at 2:48 AM, wrote:

> Ok, I found out one of my problems - if I set the project stage to
> Development, my exception gets swallowed. So changing it to Production at
> least allows a filter to catch it.
>
> The main reason for my question is that with JSF2 there is so much error
> handling/system event stuff in there that it seems a "hack" to do it via a
> filter. Obviously it can be processed and an error page sent back - heck,
> when you set the stage to "Development" a custom error page comes up.
> However it's not my error page, which is the issue.
>
> Has anybody tried just using the stuff already in JSF2 to do this, or knows
> how the RI does it so I can do something similar? It just seems a shame to
> keep using the same hacks used in the past when I'm sure I'm just missing
> something simple or obvious or both! These error handling classes were
> pushed as one of the great new features of JSF2, I just need to use how to
> use them properly!
>
> Thanks,
>
> Ian
> [Message sent by forum member 'hirowla' (ian.rowlands@three.com.au)]
>
> http://forums.java.net/jive/thread.jspa?messageID=384425
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>

--
Lincoln Baxter, III
http://ocpsoft.com
http://scrumshark.com
"Keep it Simple"
[att1.html]

hirowla
Offline
Joined: 2009-04-22

I can see what you mean, but there is the pain of getting back into the JSF "domain" after being thrown out of it by the exception. The error page I want to display is a JSF page (the problem of using Facelets is that you want to use them for everything!). You then need to add things to the FacesContext, which I know is not easily accessible from a Filter. You then need to do things like adding the exception (or extracts from it) to the session.

Not to mention that if you have your project stage as "Development", you don't get the exceptions anyway from the filter.

The filter seems a better option. I tried using a JSF page as my error page, and all that does is cause a different exception (well, it's rewrapped). I guess there's an error trying to go back into JSF when JSF threw the error in the first place.