You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ilya Kazakevich <Il...@JetBrains.com> on 2011/11/11 18:57:20 UTC

Right way for exception handling

Hello,

In bare container (tomcat) everything is good: Servlet throws exception and
container logs it and displays 500 error page with certain status.
Each uncounght exception should lead to 500 error because uncought exception
is _program_ error. 

But struts-2 breaks this scheme. 

1) it does not pass exception to container so container can't log it.
2) even if I use "exception" interceptor (ExceptionMappingInterceptor)
struts does not set "500" error code to my result. So I have to set it in my
JSP (or servlet) manually. 
I can't use static 500 error page as I did in tomcat. 

So, my questions are: 

1) Why do not struts passes exception to container by default? Could I
configure it to do so?
2) If struts can't do it -- how can I make it to send 500 status for my
exception? I do not want to configure my jsp
3) Who really needs exeption handling in struts?  Somebody wants user to be
redirected to different actions based on exception type? What for? Smells
like exception-based business logic moved from java to struts.xml and does
not look like good practice.

I need to write a lot just to get back to default container behavior (500
status and logging):

1) Create _new_ stack to set logEnabled and logLevel
2) Configure package to use it
3) Add global error result
4) Add global exception handling
5) Create JSP with "status 500" instead of my 500.html page

Is not it too big for such simple task?



Ilya Kazakevich,
Developer
JetBrains Inc
http://www.jetbrains.com
"Develop with pleasure!"


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


RE: Right way for exception handling

Posted by Ilya Kazakevich <Il...@JetBrains.com>.
Thank you.

> I'm also not sure I'd purposefully send a 500 status 
> code if I had the choice, but I guess that's up to you. The browser 
> typically gives back a pretty ugly page for such a return status, IMO.

Only IE does this. And only if "friendly" errors are enabled.
I need 500 error because human is not the only client of web application and
if you access remote server programmatically you need status code to be sure
no error takes place.
I believe it is correct to send "500" code for program errors in HTTP
environment.

Ilya.






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


Re: Right way for exception handling

Posted by Eric Lentz <Er...@sherwin.com>.
> 1) Why do not struts passes exception to container by default? Could I 
configure it to do so?

I don't have time to answer all of your questions well, but you may want 
to look here:
http://struts.apache.org/2.2.1/docs/httpheader-result.html

That result type will permit the sending of any code you want. That could 
be the basis for a global result? I'm not sure if that answers one of your 
questions or not? I'm also not sure I'd purposefully send a 500 status 
code if I had the choice, but I guess that's up to you. The browser 
typically gives back a pretty ugly page for such a return status, IMO.


Re: Right way for exception handling

Posted by Eric Lentz <Er...@sherwin.com>.
Make sure that devMode is false. The behavior is a bit different if in 
devMode.

Re: Right way for exception handling

Posted by Li Ying <li...@gmail.com>.
Hi, Ilya:

> But struts-2 breaks this scheme.
>
> 1) it does not pass exception to container so container can't log it.

I used struts2 for several years.
In our projects, struts-2 does not break this schema.

I have read the source of struts2. When an exception is thrown from
action execution, struts2 will catch it, and
(1)set 500 error to the response code
(2)put the exception instance into request attribute
[javax.servlet.error.exception] and [javax.servlet.jsp.jspException]

source code like this:

package org.apache.struts2.dispatcher;
class: Dispatcher

                // WW-1977: Only put errors in the request when code
is a 500 error
                if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
                    // send a http error response to use the servlet
defined error handler
                    // make the exception availible to the web.xml
defined error page
                    request.setAttribute("javax.servlet.error.exception", e);

                    // for compatibility
                    request.setAttribute("javax.servlet.jsp.jspException", e);
                }

                // send the error response
                response.sendError(code, e.getMessage());


you can handle exception by:
(1)add this setting into web.xml:
	<error-page>
		<exception-type>java.lang.Exception</exception-type>
		<location>/some_path/errorException.jsp</location>
	</error-page>
(2)create a errorException.jsp






2011/11/12 Ilya Kazakevich <Il...@jetbrains.com>:
> Hello,
>
> In bare container (tomcat) everything is good: Servlet throws exception and
> container logs it and displays 500 error page with certain status.
> Each uncounght exception should lead to 500 error because uncought exception
> is _program_ error.
>
> But struts-2 breaks this scheme.
>
> 1) it does not pass exception to container so container can't log it.
> 2) even if I use "exception" interceptor (ExceptionMappingInterceptor)
> struts does not set "500" error code to my result. So I have to set it in my
> JSP (or servlet) manually.
> I can't use static 500 error page as I did in tomcat.
>
> So, my questions are:
>
> 1) Why do not struts passes exception to container by default? Could I
> configure it to do so?
> 2) If struts can't do it -- how can I make it to send 500 status for my
> exception? I do not want to configure my jsp
> 3) Who really needs exeption handling in struts?  Somebody wants user to be
> redirected to different actions based on exception type? What for? Smells
> like exception-based business logic moved from java to struts.xml and does
> not look like good practice.
>
> I need to write a lot just to get back to default container behavior (500
> status and logging):
>
> 1) Create _new_ stack to set logEnabled and logLevel
> 2) Configure package to use it
> 3) Add global error result
> 4) Add global exception handling
> 5) Create JSP with "status 500" instead of my 500.html page
>
> Is not it too big for such simple task?
>
>
>
> Ilya Kazakevich,
> Developer
> JetBrains Inc
> http://www.jetbrains.com
> "Develop with pleasure!"
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

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