ExampleErrorForwardHandler implements Handler<Object, Object> { @Override public Object handle(Object data, ExecutionContext context) { try { return context.handleNext(data); } catch (SessionKeyNotFoundException e) { throw new HttpErrorResponse(HttpResponse.Status.BAD_REQUEST.getStatusCode(), "/WEB-INF/view/common/errorPages/doubleSubmissionError.jsp", e); } catch (NoDataException e) { throw new HttpErrorResponse(HttpResponse.Status.NOT_FOUND.getStatusCode(), "/WEB-INF/view/common/errorPages/pageNotFoundError.jsp", e); } catch (OptimisticLockException e) { throw new HttpErrorResponse(HttpResponse.Status.BAD_REQUEST.getStatusCode(), "/WEB-INF/view/common/errorPages/optimisticLockError.jsp", e); } catch (TemporaryFileFailedException e) { FailureLogUtil.logFatal(e, context.getDataProcessedWhenThrown(e), null); throw new HttpErrorResponse(HttpResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); } }  @Override Object handle(Object data, ExecutionContext context);  }
@Test public void success_shouldReturnHttpResponse() throws Exception { ExecutionContext context = new ExecutionContext(); context.addHandler((o, context1) -> new HttpResponse("success.jsp")); final HttpResponse actual = (HttpResponse) sut.handle(null, context); assertThat(actual.getContentPath().getPath(), is("success.jsp")); }
@Test public void sessionKeyNotFound_shouldThrowDoubleSubmissionErrorPage() throws Exception { ExecutionContext context = new ExecutionContext(); context.addHandler((o, context1) -> { throw new SessionKeyNotFoundException("key"); }); expectedException.expect(HttpErrorResponse.class); expectedException.expect( ContentPathMatcher.hasContentPath("/WEB-INF/view/common/errorPages/doubleSubmissionError.jsp")); expectedException.expect(StatusCodeMatcher.isStatusCode(400)); sut.handle(new Object(), context); }
@Test public void noDataFound_shouldThrowPageNotFoundPage() throws Exception { ExecutionContext context = new ExecutionContext(); context.addHandler((o, context1) -> { throw new NoDataException(); }); expectedException.expect(HttpErrorResponse.class); expectedException.expect( ContentPathMatcher.hasContentPath("/WEB-INF/view/common/errorPages/pageNotFoundError.jsp")); expectedException.expect(StatusCodeMatcher.isStatusCode(404)); sut.handle(new Object(), context); }
@Test public void optimisticLockException_shouldThrowOptimisticLockErrorPage() throws Exception { ExecutionContext context = new ExecutionContext(); context.addHandler((o, context1) -> { throw new OptimisticLockException(); }); expectedException.expect(HttpErrorResponse.class); expectedException.expect( ContentPathMatcher.hasContentPath("/WEB-INF/view/common/errorPages/optimisticLockError.jsp")); expectedException.expect(StatusCodeMatcher.isStatusCode(400)); sut.handle(new Object(), context); }
@Test public void failedToTempFile_shouldThrowInternalError() throws Exception { ExecutionContext context = new ExecutionContext(); context.addHandler((o, context1) -> { throw new TemporaryFileFailedException(new Throwable()); }); expectedException.expect(HttpErrorResponse.class); expectedException.expect(StatusCodeMatcher.isStatusCode(500)); sut.handle(new Object(), context); }