@AroundInvoke
public Object processException(InvocationContext ctx) throws Exception {
Method m = ctx.getMethod();
// FIXME would be helpful if Catch provided a utility to get the CaughtException
if (ctx.getParameters().length > 0 && ctx.getParameters()[0] instanceof CaughtException) {
HttpServletRequestContext requestCtx = requestCtxResolver.get();
// we can't respond w/ this exception handler if response is committed
// TODO should see if exception is marked handled already
if (requestCtx.getResponse().isCommitted()) {
return Void.TYPE;
}
CaughtException<?> c = (CaughtException<?>) ctx.getParameters()[0];
if (m.isAnnotationPresent(SendHttpError.class)) {
SendHttpError r = m.getAnnotation(SendHttpError.class);
String message = r.message().trim();
if (r.message().length() == 0 && r.useExceptionMessageAsDefault()) {
message = c.getException().getMessage();
}
if (message != null && message.length() > 0) {
requestCtx.getResponse().sendError(r.status(), message);
} else {
requestCtx.getResponse().sendError(r.status());
}
} else if (m.isAnnotationPresent(SendErrorPage.class)) {
SendErrorPage r = m.getAnnotation(SendErrorPage.class);
if (r.redirect()) {
requestCtx.getResponse().sendRedirect(requestCtx.getResponse().encodeRedirectURL(r.value()));
} else {
requestCtx.getRequest().getRequestDispatcher(r.value())
.forward(requestCtx.getRequest(), requestCtx.getResponse());
}
} else {
// perhaps log a warning?
}
}