You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by ad...@apache.org on 2014/12/29 14:47:53 UTC

wicket git commit: WICKET-5794 Make DefaultExceptionMapper extensible

Repository: wicket
Updated Branches:
  refs/heads/master 802b45517 -> abbc45100


WICKET-5794 Make DefaultExceptionMapper extensible

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/abbc4510
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/abbc4510
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/abbc4510

Branch: refs/heads/master
Commit: abbc45100e627937f297ee69c71dd6458ea76667
Parents: 802b455
Author: Andrea Del Bene <ad...@apache.org>
Authored: Mon Dec 29 13:06:53 2014 +0100
Committer: Andrea Del Bene <ad...@apache.org>
Committed: Mon Dec 29 13:06:53 2014 +0100

----------------------------------------------------------------------
 .../apache/wicket/DefaultExceptionMapper.java   | 109 ++++++++++++++-----
 1 file changed, 79 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/abbc4510/wicket-core/src/main/java/org/apache/wicket/DefaultExceptionMapper.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/DefaultExceptionMapper.java b/wicket-core/src/main/java/org/apache/wicket/DefaultExceptionMapper.java
index e3b89fd..1faae85 100644
--- a/wicket-core/src/main/java/org/apache/wicket/DefaultExceptionMapper.java
+++ b/wicket-core/src/main/java/org/apache/wicket/DefaultExceptionMapper.java
@@ -75,7 +75,14 @@ public class DefaultExceptionMapper implements IExceptionMapper
 			return new ErrorCodeRequestHandler(500);
 		}
 	}
-
+	
+	/**
+	 * Maps exceptions to their corresponding {@link IRequestHandler}.
+	 * 
+	 * @param e
+	 * 			the current exception
+	 * @return the {@link IRequestHandler} for the current exception
+	 */
 	private IRequestHandler internalMap(Exception e)
 	{
 		final Application application = Application.get();
@@ -90,6 +97,29 @@ public class DefaultExceptionMapper implements IExceptionMapper
 			}
 		}
 
+		IRequestHandler handleExpectedExceptions = mapExpectedExceptions(e, application);
+		
+		if (handleExpectedExceptions != null)
+		{
+			return handleExpectedExceptions;
+		}
+
+		return mapUnexpectedExceptions(e, application);
+	
+	}
+	
+	/**
+	 * Maps expected exceptions (i.e. those internally used by Wicket) to their corresponding
+	 * {@link IRequestHandler}.
+	 * 
+	 * @param e
+	 * 			the current exception
+	 * @param application
+	 * 			the current application object
+	 * @return the {@link IRequestHandler} for the current exception
+	 */
+	protected IRequestHandler mapExpectedExceptions(Exception e, final Application application)
+	{
 		if (e instanceof StalePageException)
 		{
 			// If the page was stale, just re-render it
@@ -119,34 +149,53 @@ public class DefaultExceptionMapper implements IExceptionMapper
 			logger.debug(e.getMessage(), e);
 			return new ErrorCodeRequestHandler(404);
 		}
-		else
-		{
+		
+		return null;
+	}
 
-			final ExceptionSettings.UnexpectedExceptionDisplay unexpectedExceptionDisplay = application.getExceptionSettings()
-				.getUnexpectedExceptionDisplay();
+	/**
+	 * Maps unexpected exceptions to their corresponding {@link IRequestHandler}.
+	 * 
+	 * @param e
+	 * 			the current exception
+	 * @param application
+	 * 			the current application object
+	 * @return the {@link IRequestHandler} for the current exception
+	 */
+	protected IRequestHandler mapUnexpectedExceptions(Exception e, final Application application)
+	{
+		final ExceptionSettings.UnexpectedExceptionDisplay unexpectedExceptionDisplay = application.getExceptionSettings()
+			.getUnexpectedExceptionDisplay();
 
-			logger.error("Unexpected error occurred", e);
+		logger.error("Unexpected error occurred", e);
 
-			if (ExceptionSettings.SHOW_EXCEPTION_PAGE.equals(unexpectedExceptionDisplay))
-			{
-				Page currentPage = extractCurrentPage();
-				return createPageRequestHandler(new PageProvider(new ExceptionErrorPage(e,
-					currentPage)));
-			}
-			else if (ExceptionSettings.SHOW_INTERNAL_ERROR_PAGE.equals(unexpectedExceptionDisplay))
-			{
-				return createPageRequestHandler(new PageProvider(
-					application.getApplicationSettings().getInternalErrorPage()));
-			}
-			else
-			{
-				// IExceptionSettings.SHOW_NO_EXCEPTION_PAGE
-				return new ErrorCodeRequestHandler(500);
-			}
+		if (ExceptionSettings.SHOW_EXCEPTION_PAGE.equals(unexpectedExceptionDisplay))
+		{
+			Page currentPage = extractCurrentPage();
+			return createPageRequestHandler(new PageProvider(new ExceptionErrorPage(e,
+				currentPage)));
 		}
+		else if (ExceptionSettings.SHOW_INTERNAL_ERROR_PAGE.equals(unexpectedExceptionDisplay))
+		{
+			return createPageRequestHandler(new PageProvider(
+				application.getApplicationSettings().getInternalErrorPage()));
+		}
+		
+		// IExceptionSettings.SHOW_NO_EXCEPTION_PAGE
+		return new ErrorCodeRequestHandler(500);
 	}
 
-	private RenderPageRequestHandler createPageRequestHandler(PageProvider pageProvider)
+	/**
+	 * Creates a {@link RenderPageRequestHandler} for the target page provided by {@code pageProvider}.
+	 * 
+	 * Uses {@link RenderPageRequestHandler.RedirectPolicy.NEVER_REDIRECT} policy to preserve the original page's URL 
+	 * for non-Ajax requests and {@link RenderPageRequestHandler.RedirectPolicy.AUTO_REDIRECT} for AJAX requests.
+	 * 
+	 * @param pageProvider
+	 * 			the page provider for the target page
+	 * @return the request handler for the target page
+	 */
+	protected final RenderPageRequestHandler createPageRequestHandler(PageProvider pageProvider)
 	{
 		RequestCycle requestCycle = RequestCycle.get();
 
@@ -156,10 +205,6 @@ public class DefaultExceptionMapper implements IExceptionMapper
 				"there is no current request cycle attached to this thread");
 		}
 
-		/*
-		 * Use NEVER_REDIRECT policy to preserve the original page's URL for non-Ajax requests and
-		 * always redirect for ajax requests
-		 */
 		RenderPageRequestHandler.RedirectPolicy redirect = RenderPageRequestHandler.RedirectPolicy.NEVER_REDIRECT;
 
 		if (isProcessingAjaxRequest())
@@ -169,8 +214,12 @@ public class DefaultExceptionMapper implements IExceptionMapper
 
 		return new RenderPageRequestHandler(pageProvider, redirect);
 	}
-
-	private boolean isProcessingAjaxRequest()
+	
+	/**
+	 * 
+	 * @return true if current request is an AJAX request, false otherwise.
+	 */
+	protected final boolean isProcessingAjaxRequest()
 	{
 		RequestCycle rc = RequestCycle.get();
 		Request request = rc.getRequest();
@@ -185,7 +234,7 @@ public class DefaultExceptionMapper implements IExceptionMapper
 	 * @return the page being rendered when the exception was thrown, or {@code null} if it cannot
 	 *         be extracted
 	 */
-	private Page extractCurrentPage()
+	protected final Page extractCurrentPage()
 	{
 		final RequestCycle requestCycle = RequestCycle.get();