Allow the application to customize the conversion of arbitrary error messages
to friendly error text. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1758 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -24,6 +24,7 @@ import java.util.Properties;
|
|||||||
|
|
||||||
import javax.servlet.ServletConfig;
|
import javax.servlet.ServletConfig;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.apache.velocity.app.Velocity;
|
import org.apache.velocity.app.Velocity;
|
||||||
|
|
||||||
@@ -34,6 +35,7 @@ import com.samskivert.servlet.MessageManager;
|
|||||||
import com.samskivert.servlet.RedirectException;
|
import com.samskivert.servlet.RedirectException;
|
||||||
import com.samskivert.servlet.SiteIdentifier;
|
import com.samskivert.servlet.SiteIdentifier;
|
||||||
import com.samskivert.servlet.SiteResourceLoader;
|
import com.samskivert.servlet.SiteResourceLoader;
|
||||||
|
import com.samskivert.servlet.util.ExceptionMap;
|
||||||
import com.samskivert.util.StringUtil;
|
import com.samskivert.util.StringUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,6 +45,16 @@ import com.samskivert.util.StringUtil;
|
|||||||
* represents the web application. The application class is responsible
|
* represents the web application. The application class is responsible
|
||||||
* for initializing services that will be used by the application's logic
|
* for initializing services that will be used by the application's logic
|
||||||
* objects as well as cleaning them up when the application is shut down.
|
* objects as well as cleaning them up when the application is shut down.
|
||||||
|
*
|
||||||
|
* <p><b>Error handling</b><br>
|
||||||
|
* The application provides a common error handling mechanism. The design is to
|
||||||
|
* catch any exceptions thrown by the logic and to convert them into friendly
|
||||||
|
* error messages that are inserted into the invocation context with the key
|
||||||
|
* <code>"error"</code> for easy display in the resulting web page.
|
||||||
|
*
|
||||||
|
* <p> The default process of mapping exceptions to friendly error messages is
|
||||||
|
* done using the {@link ExceptionMap} class. This can be replaced by
|
||||||
|
* overriding {@link #handleException}.
|
||||||
*/
|
*/
|
||||||
public class Application
|
public class Application
|
||||||
{
|
{
|
||||||
@@ -198,6 +210,22 @@ public class Application
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If a generic exception propagates up from {@link Logic#invoke} and is
|
||||||
|
* not otherwise converted into a friendly or redirect exception, the
|
||||||
|
* application will be required to provide a generic error message to be
|
||||||
|
* inserted into the context and should take this opportunity to log the
|
||||||
|
* exception.
|
||||||
|
*/
|
||||||
|
protected String handleException (
|
||||||
|
HttpServletRequest req, Logic logic, Exception error)
|
||||||
|
{
|
||||||
|
Log.warning("Choked on request [logic=" + logic +
|
||||||
|
", req=" + req.getRequestURI() + "].");
|
||||||
|
Log.logStackTrace(error);
|
||||||
|
return ExceptionMap.getMessage(error);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This should be overridden by the application implementation to
|
* This should be overridden by the application implementation to
|
||||||
* perform any necessary cleanup.
|
* perform any necessary cleanup.
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ import com.samskivert.servlet.MessageManager;
|
|||||||
import com.samskivert.servlet.RedirectException;
|
import com.samskivert.servlet.RedirectException;
|
||||||
import com.samskivert.servlet.SiteIdentifier;
|
import com.samskivert.servlet.SiteIdentifier;
|
||||||
import com.samskivert.servlet.SiteResourceLoader;
|
import com.samskivert.servlet.SiteResourceLoader;
|
||||||
import com.samskivert.servlet.util.ExceptionMap;
|
|
||||||
import com.samskivert.servlet.util.FriendlyException;
|
import com.samskivert.servlet.util.FriendlyException;
|
||||||
|
|
||||||
import com.samskivert.util.ConfigUtil;
|
import com.samskivert.util.ConfigUtil;
|
||||||
@@ -136,19 +135,7 @@ import com.samskivert.util.StringUtil;
|
|||||||
* inserted in the future (ie. if I ever want to use this to develop a
|
* inserted in the future (ie. if I ever want to use this to develop a
|
||||||
* cobranded web site).
|
* cobranded web site).
|
||||||
*
|
*
|
||||||
* <p><b>Error handling</b><br>
|
|
||||||
* The dispatcher servlet provides a common error handling mechanism. The
|
|
||||||
* design is to catch any exceptions thrown by the logic and to convert
|
|
||||||
* them into friendly error messages that are inserted into the invocation
|
|
||||||
* context with the key <code>"error"</code> for easy display in the
|
|
||||||
* resulting web page.
|
|
||||||
*
|
|
||||||
* <p> The process of mapping exceptions to friendly error messages is
|
|
||||||
* done using the {@link ExceptionMap} class. Consult its documentation
|
|
||||||
* for an explanation of how it works.
|
|
||||||
*
|
|
||||||
* @see Logic
|
* @see Logic
|
||||||
* @see ExceptionMap
|
|
||||||
*/
|
*/
|
||||||
public class DispatcherServlet extends VelocityServlet
|
public class DispatcherServlet extends VelocityServlet
|
||||||
implements MethodExceptionEventHandler
|
implements MethodExceptionEventHandler
|
||||||
@@ -382,10 +369,7 @@ public class DispatcherServlet extends VelocityServlet
|
|||||||
errmsg = fe.getMessage();
|
errmsg = fe.getMessage();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
errmsg = ExceptionMap.getMessage(e);
|
errmsg = _app.handleException(req, logic, e);
|
||||||
Log.warning("Choked on request [logic=" + logic +
|
|
||||||
", req=" + req.getRequestURI() + "].");
|
|
||||||
Log.logStackTrace(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have an error message, insert it into the template
|
// if we have an error message, insert it into the template
|
||||||
|
|||||||
Reference in New Issue
Block a user