Issue Details (XML | Word | Printable)

Key: WW-558
Type: New Feature New Feature
Status: Closed Closed
Resolution: Duplicate
Priority: Major Major
Assignee: Jason Carreira
Reporter: John Patterson
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
WebWork

Exception Handler Interceptor and Exception Action

Created: 11/Jun/04 11:07 AM   Updated: 03/Jul/05 01:39 PM
Component/s: Interceptors
Affects Version/s: 2.2
Fix Version/s: 2.2

File Attachments: 1. Java Source File ExceptionInterceptor.java (4 kB)

Issue Links:
Duplicate
 
Related
 


 Description  « Hide
The interceptor and action I use to handle exceptions

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
John Patterson added a comment - 11/Jun/04 11:14 AM
The ExceptionAction

/*
 * Created on 04-Nov-2003
 */

package com.buildabreak.actions;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.ServletResponse;
import javax.servlet.jsp.PageContext;

import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;

/**
 * Displays the SUCCESS page and makes the stack trace available as a string
 * property. If the response is already committed then all we can do is print an error
 * message on the page.
 *
 * @author John Patterson
 */
public class ExceptionAction extends BaseAction
{
private Exception _exception;

public ExceptionAction()
{
_exception = (Exception) ActionContext.getContext().get("exception");
}

public String getStackTrace()
{
if (_exception != null)
{
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
_exception.printStackTrace(printWriter);

return stringWriter.toString();
}
else
{
return null;
}
}

/**
* @return Returns the exception.
*/
public Exception getException()
{
return _exception;
}

/*
* (non-Javadoc)
*
* @see com.opensymphony.xwork.Action#execute()
*/
public String execute() throws Exception
{
PageContext context = ServletActionContext.getPageContext();
ServletResponse response = ServletActionContext.getResponse();

if (response.isCommitted())
{
Writer writer;
if (context != null)
{
writer = context.getOut();
}
else
{
writer = response.getWriter();
}
writer.write("<b>An error occured during the request.</b>");
writer.flush();
return NONE;
}
else
{
return SUCCESS;
}
}
}

John Patterson added a comment - 11/Jun/04 11:16 AM
The Interceptor

NB: All my actions override toString() so the log message contains useful state information.

/*
 * Created on 04-Nov-2003
 */
package shared.webwork.interceptor;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionInvocation;
import com.opensymphony.xwork.interceptor.Interceptor;

/**
 * Catches any exceptions that occur during the invocation and returns
 * Action.ERROR as the result code. The exception and the Action are logged.
 *
 * @author John Patterson
 */
public class ExceptionHandlerInterceptor implements Interceptor
{
  private static Logger _logger = Logger.getLogger(ExceptionHandlerInterceptor.class.getName());
  
  /*
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
   */
  public String intercept(ActionInvocation invocation) throws Exception
  {
    try
    {
      return invocation.invoke();
    }
    catch (Exception e)
    {
      if (!invocation.isExecuted())
      {
        // the result was not executed
        String newLine = System.getProperty("line.separator");
        String message = "Caught exception while invoking action.";
        message += newLine + "Action was: " + invocation.getAction();
        _logger.log(Level.SEVERE, message, e);
        ActionContext.getContext().put("exception", e);
      }
      return Action.ERROR;
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork.interceptor.Interceptor#destroy()
   */
  public void destroy()
  {
  }

  /*
   * (non-Javadoc)
   *
   * @see com.opensymphony.xwork.interceptor.Interceptor#init()
   */
  public void init()
  {
  }
}

Matthew Porter added a comment - 21/Mar/05 10:33 AM
These two issues are similar and will be addressed in XW 1.1/WW2.2

sutter2k added a comment - 02/Jun/05 09:17 AM
Here is another flavor of an exception interceptor. Similiar to the one used in Webwork in Action.

However, this one allows the developer to have explainations of exceptions in the properties "resource" for a given action.

eg.

exception."methodName"."exceptionName"

// example key -->exception.save.ServiceException=there is a problem with your service dude
or
properties names should look like