This may be too ambiguous of a question, but I'm looking for some help or best practices on handling exceptions in a JavaServer Faces application.
Right now, whenever I catch an exception, I log it and then throw another exception that I created, say MyCustomException
. This custom exception takes the Throwable
object that was caught and emails the stack trace to me so that I know an exception has occurred. For now, this seems to work for me as far as being notified when an exception occurs.
However, the trouble that I'm having is how to present the user with a pretty error page that lets them know that something went wrong and that I have been notified of the error. Doing some searching, I've come across several articles that show how to redirect a user to another page using something similar to the following:
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext.getRequestContextPath() +
"/messages.faces");
extContext.redirect(url);
But this seems to be quite a bit to basically copy and paste for each caught exception, which doesn't exactly follow the DRY principle. In the end, I think what I would like to do is have an email sent to me whenever an exception occurs and then be able to send a message to a generic error page that then displays the message to the user.
Any ideas or best practices that I should follow? Thank you in advance for any advice you can give! I'm stumped.
Note: If it helps, I'm using JSF 2.0 and Facelets.