You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sébastien GALLET <se...@gallet.info> on 2005/04/24 15:11:14 UTC

Managing exceptions

Hello,
I'm looking for a good way to manage exceptions in struts.
Any links in your bookmarks ?
TIA

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Managing exceptions

Posted by Joe Germuska <Jo...@Germuska.com>.
At 5:57 PM +0200 4/28/05, Sébastien GALLET wrote:
>Joe Germuska a écrit :
>
>>At 3:11 PM +0200 4/24/05, Sébastien GALLET wrote:
>>
>>>Hello,
>>>I'm looking for a good way to manage exceptions in struts.
>>>Any links in your bookmarks ?
>>
>>
>>That's a pretty general question!
>
>yes, it was :-[
>With your help, I can catch exceptions in my 
>servlet and display them in my app.
>Is there a way to catch jsp exception (caused by 
>a missing resource for examples) and tiles 
>exception ?

Check out <c:catch> from the JSTL: 
http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/catch.html

Struts itself doesn't provide any support, 
because the ExceptionHandler which is distributed 
in the core always assumes that its safe to 
forward -- which is illegal once the response is 
committed, as when you have a tiles exception.

I don't think it would be too much work for 
someone to adjust the Struts general exception 
handler so that it tested response.isCommitted() 
and somehow did a request dispatcher include 
instead of returning an ActionForward.  It would 
take a bit of thinking to flush out possible 
wrinkles, but its conceptually pretty simple, 
except for determining how to best configure it.

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Managing exceptions

Posted by Sébastien GALLET <se...@gallet.info>.
Joe Germuska a écrit :

> At 3:11 PM +0200 4/24/05, Sébastien GALLET wrote:
>
>> Hello,
>> I'm looking for a good way to manage exceptions in struts.
>> Any links in your bookmarks ?
>
>
> That's a pretty general question!

yes, it was :-[
With your help, I can catch exceptions in my servlet and display them in 
my app.
Is there a way to catch jsp exception (caused by a missing resource for 
examples) and tiles exception ?

> Hope this helps,

Yes it does

>     Joe
>
Sebastien

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Managing exceptions

Posted by Mark Shifman <ma...@yale.edu>.
I am using struts 1.2.6 and I tried what Joe Germuska said however I am 
missing something.
The message shows up so that <html:errors/> displays it.

Or to put it another way
<html:messages id="message" message="false">
    <font color="red">${message}</font><BR>
</html:messages>

displays it while

<html:messages id="message" message="true">
    <font color="red">${message}</font><BR>
</html:messages>

doesn't.  So it looks like the exception is generating an ActionErrors 
object.

I generated an sql error for testing which throws a RuntimeException.

What am I missing??

mas

 

Joe Germuska wrote:

> At 3:11 PM +0200 4/24/05, Sébastien GALLET wrote:
>
>> Hello,
>> I'm looking for a good way to manage exceptions in struts.
>> Any links in your bookmarks ?
>
>
> That's a pretty general question!
>
> I usually start by having a single global exception handler defined 
> which catches anything thrown by Struts:
>
>     <global-exceptions>
>         <exception
>             key="GlobalExceptionHandler.default"
>             type="java.lang.Exception"
>             path="/ErrorPage.jsp"
>             handler="org.apache.struts.action.ExceptionHandler"
>             />
>     </global-exceptions>
>
> This ensures that any exceptions get handled by my app instead of the 
> servlet container.  The value for "key" points to a message key in 
> your message resources; mine usually says something like "An 
> unexpected error occurred."
>
> When it catches exceptions, he default Struts ExceptionHandler (as 
> configured above) does three things:
>
> * Puts the exception into the request scope under they key 
> "org.apache.struts.action.EXCEPTION"
> * puts an ActionMessages object into the request scope (by default) or 
> the session scope (if the "scope" attribute of <exception> is set to 
> "session").  This ActionMessages contains a single ActionMessage which 
> is created in one of two ways:
>     - If the caught exception extends o.a.s.util.ModuleException, that 
> type's getActionMessage method is called
>     - otherwise, a new ActionMessage is created using the "key" 
> attribute of the <exception> config and passing the exception in as 
> the single
> * Forwards control to a view renderer in one of two ways:
>     - If the "path" attribute of <exception> is specified, the forward 
> goes there
>     - otherwise, the "input forward" for the current request is used.
>
> and forwards control to the path specified in the exception-config 
> (above, "/ErrorPage.jsp")  If the Exception which is caught extends 
> org.apache.struts.util.ModuleException, then that class's 
> "getActionMessage" method is used to populate the ActionMessages which 
> is going into the request; otherwise, the "key" from the 
> exception-config mapping is used to create a new ActionMessage, and 
> the thrown exception is included as a message format argument.
>
> So, this basic approach covers at least a way to present users with a 
> page which looks like it belongs in your application, as opposed to a 
> generic container error page; then if you want to provide multiple 
> mappings (either global or per-action-mapping), you can refine this 
> general strategy.  You can also extend ExceptionHandler and introduce 
> your own error logging behavior or other specialized operations.
>
> Hope this helps,
>     Joe
>


-- 
 Mark Shifman MD. Ph.D.
 Yale Center for Medical Informatics
 Phone (203)737-5219
 mark.shifman@yale.edu


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Managing exceptions

Posted by Joe Germuska <Jo...@Germuska.com>.
At 3:11 PM +0200 4/24/05, Sébastien GALLET wrote:
>Hello,
>I'm looking for a good way to manage exceptions in struts.
>Any links in your bookmarks ?

That's a pretty general question!

I usually start by having a single global 
exception handler defined which catches anything 
thrown by Struts:

	<global-exceptions>
		<exception
			key="GlobalExceptionHandler.default"
			type="java.lang.Exception"
			path="/ErrorPage.jsp"
			handler="org.apache.struts.action.ExceptionHandler"
			/>
	</global-exceptions>

This ensures that any exceptions get handled by 
my app instead of the servlet container.  The 
value for "key" points to a message key in your 
message resources; mine usually says something 
like "An unexpected error occurred."

When it catches exceptions, he default Struts 
ExceptionHandler (as configured above) does three 
things:

* Puts the exception into the request scope under 
they key "org.apache.struts.action.EXCEPTION"
* puts an ActionMessages object into the request 
scope (by default) or the session scope (if the 
"scope" attribute of <exception> is set to 
"session").  This ActionMessages contains a 
single ActionMessage which is created in one of 
two ways:
     - If the caught exception extends 
o.a.s.util.ModuleException, that type's 
getActionMessage method is called
     - otherwise, a new ActionMessage is created 
using the "key" attribute of the <exception> 
config and passing the exception in as the single
* Forwards control to a view renderer in one of two ways:
     - If the "path" attribute of <exception> is 
specified, the forward goes there
     - otherwise, the "input forward" for the current request is used.

and forwards control to the path specified in the 
exception-config (above, "/ErrorPage.jsp")  If 
the Exception which is caught extends 
org.apache.struts.util.ModuleException, then that 
class's "getActionMessage" method is used to 
populate the ActionMessages which is going into 
the request; otherwise, the "key" from the 
exception-config mapping is used to create a new 
ActionMessage, and the thrown exception is 
included as a message format argument.

So, this basic approach covers at least a way to 
present users with a page which looks like it 
belongs in your application, as opposed to a 
generic container error page; then if you want to 
provide multiple mappings (either global or 
per-action-mapping), you can refine this general 
strategy.  You can also extend ExceptionHandler 
and introduce your own error logging behavior or 
other specialized operations.

Hope this helps,
	Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
"Narrow minds are weapons made for mass destruction"  -The Ex

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org