DefaultRequestExceptionHandler implements RequestExceptionHandler { @SuppressWarnings({"unchecked", "rawtypes"}) protected Object[] formExceptionContext(Throwable exception) { if (exception instanceof ContextAwareException) return ((ContextAwareException) exception).getContext(); Class exceptionClass = exception.getClass(); while ("".equals(exceptionClass.getSimpleName())) exceptionClass = exceptionClass.getSuperclass(); if (exceptionClass.isAssignableFrom(RuntimeException.class)) return exception.getMessage() == null ? new Object[0] : new Object[]{exception.getMessage().toLowerCase()}; String exceptionType = exceptionClass.getSimpleName(); if (exceptionType.endsWith("Exception")) exceptionType = exceptionType.substring(0, exceptionType.length() - 9); return new Object[]{exceptionType.toLowerCase()}; } @SuppressWarnings("rawtypes")  DefaultRequestExceptionHandler(RequestPageCache pageCache,
                                          PageResponseRenderer renderer,
                                          Logger logger,
                                          @Symbol(SymbolConstants.EXCEPTION_REPORT_PAGE)
                                          String pageName,
                                          Request request,
                                          Response response,
                                          ComponentClassResolver componentClassResolver,
                                          LinkSource linkSource,
                                          ServiceResources serviceResources,
                                          ExceptionReporter exceptionReporter,
                                          Map<Class, Object> configuration); @SuppressWarnings({"rawtypes", "unchecked"}) void handleRequestException(Throwable exception);  }
@Test public void noContextWhenExceptionDoesntContainMessage() { Object[] context = exceptionHandler.formExceptionContext(new RuntimeException() { }); assertEquals(context.length, 0); }
@Test public void contextIsExceptionMessage() { Object[] context = exceptionHandler.formExceptionContext(new RuntimeException() { public String getMessage() { return "HelloWorld"; } }); assertEquals(context.length, 1); assertTrue("helloworld".equals(context[0])); }
@Test public void contextIsExceptionType() { Object[] context = exceptionHandler.formExceptionContext(new IllegalArgumentException("Value not allowed")); assertEquals(context.length, 1); assertTrue(context[0] instanceof String); assertTrue("illegalargument".equals(context[0])); }
@Test public void contextIsProvidedByContextAwareException() { Object[] sourceContext = new Object[]{new Integer(10), this}; Object[] context = exceptionHandler.formExceptionContext(new MyContextAwareException(sourceContext) { }); assertEquals(context, sourceContext); }