struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frank W. Zammetti" <fzli...@omnytex.com>
Subject Re: Getting at exceptions in Action Classes when forwardedfromweb.xml?
Date Mon, 03 Oct 2005 22:59:57 GMT
I dont know if this is how you *have* to do it, but it's how I do it... 
I have the same requirements you enumerate here, and this is how I 
accomplished it.  Works very well.  What you have essentially is a class 
that looks a lot like an Action (but isn't actually one... look at the 
execute() signature... identical except for the Excepion being passed in 
too) that you can do whatever you like in.  Then you forward to a JSP.

Here's an example from one of my apps, cut down a bit... probably more 
than your really interested in, but it gives you an idea of what you can 
do...


public class GlobalExceptionHandler extends ExceptionHandler {
   public ActionForward execute(Exception e, ExceptionConfig config,
                                ActionMapping mapping, ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
                                throws ServletException {
     if (log.isDebugEnabled()) {
       log.debug("GlobalExceptionHandler.execute(): " + e);
     }
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     e.printStackTrace(new PrintStream(baos));
     HashMap attributes = AppHelpers.getRequestAttributes(request, true);
     HashMap parameters = AppHelpers.getRequestParameters(request, true);
     HashMap headers = AppHelpers.getRequestHeaders(request);
     HashMap sessionAttributes = 
AppHelpers.getSessionAttributes(request, true);
     request.setAttribute("error_attributes", attributes);
     request.setAttribute("error_parameters", parameters);
     request.setAttribute("error_headers", headers);
     request.setAttribute("error_session_attributes", sessionAttributes);
     HttpSession session = request.getSession(false);
     String userID     = "Unknown";
     String clientCode = "Unknown";
     if (session != null) {
       HashMap userInfo = (HashMap)session.getAttribute("userInfo");
       if (userInfo != null) {
         userID = (String)userInfo.get("userID");
       } else {
         userID = "userInfo was null, could not retreive";
       }
       HashMap activeClientInfo =
         (HashMap)session.getAttribute("activeClientInfo");
       if (activeClientInfo != null) {
         clientCode = (String)activeClientInfo.get("clientCode");
       } else {
         clientCode = "clientCode was null, could not retreive";
       }
     } else {
       userID = "session was null, could not retrieve";
       clientCode = "session was null, could not retrieve";
     }
     String path = "Unknown";
     if (mapping != null) {
       path = mapping.getPath();
     } else {
       path = "mapping was null, could not retreive";
     }
     String node = "Unknown";
     try {
       InetAddress addr = InetAddress.getLocalHost();
       node = addr.getHostName();
     } catch (Exception ee) {
       node = "exception occurred, could not retrieve";
     }
     StringBuffer sb = new StringBuffer(4096);
     sb.append("Exception in Action - Resequencing to exception page...\n");
     sb.append("Date/Time: " + new java.util.Date() + "\n");
     sb.append("userID: " + userID + "\n");
     sb.append("path: " + path + "\n");
     sb.append("node: " + node + "\n\n");
     sb.append("Stack Trace: " + baos + "\n\n");
     sb.append("\nRequest Attributes: " + attributes + "\n\n");
     sb.append("\nRequest Parameters: " + parameters + "\n\n");
     sb.append("\nRequest Headers: " + headers + "\n\n");
     sb.append("\nSession Attributes: " + sessionAttributes + "\n");
     log.error(sb);
     return mapping.findForward("exception");
   }
}


The exception mapping is a global forward that winds me up in 
eception.jsp, which displays most of the information you see dumped to 
the log above (not all of it... some of it is potentially sensitive). 
You get the idea though.  This will handle any exception on the 
server-side, except for JSP error.  But, obviously you shouldn't ever 
get a JSP compile-time error in a production app, and if you've coded it 
half-way decently you should never get a runtime exception either (I 
can't remember the last time I saw a runtime JSP exception).

Frank

Preston CRAWFORD wrote:
> Is this what I have to do?
> 
> All I want to do is throw up a default error page that looks like the
> rest of the site, but ALSO log the error, no matter what kind it is.
> Nothing more.
> 
> Preston
> 
> 
>>>>fzlists@omnytex.com 10/3/2005 3:09:05 PM >>>
> 
> What version of Struts are you using?  Have a look at this:
> 
> http://struts.apache.org/userGuide/building_controller.html#exception_handler
> 
> 
> That's probably the way you want to go, might save you some trouble. 
> I'm not sure when it was introduced though, so if your a ways behind in
> 
> Struts version it might not be available.
> 
> Frank
> 
> Adam Hardy wrote:
> 
>>Preston CRAWFORD on 03/10/05 21:47, wrote:
>>
>>
>>>I'm trying to setup global exception handling. I have web.xml
> 
> sending
> 
>>>404 and 500 exceptions to /error.do. In that action I should be able
> 
> to
> 
>>>get at the exception, shouldn't I? Or do I have to do that earlier
> 
> in
> 
>>>the process? I simply want to get at the exception and log it.
>>>
>>
>>I don't think you can. The response object is write-only, so there is
> 
> no 
> 
>>way to tell.
>>
>>I might be grossly mistaken but I guess you would have to have
> 
> seperate 
> 
>>error-pages for each type of exception. Haven't tried it myself but
> 
> will 
> 
>>need to soon.
>>
>>Adam
>>
>>
> 
> ---------------------------------------------------------------------
> 
>>To unsubscribe, e-mail: user-unsubscribe@struts.apache.org 
>>For additional commands, e-mail: user-help@struts.apache.org 
>>
>>
>>
>>
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com
AIM: fzammetti
Yahoo: fzammetti
MSN: fzammetti@hotmail.com

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


Mime
View raw message