I have a factory method that takes a request parameter, and loads an entity. My problem relates to what happens when the factory is passed an id that is not a valid value, and how it is handled.
One option is to throw the entity not found exception which then redirects to a single error page. Nice solution except that the page you want to end up on could vary depending on the entity you were trying to load. I.e. Go back to the widget list if you can't find the widget. This could be gotten around by using custom exceptions (i.e. WidgetNotFoundException), which would also maintain the degree of loose coupling rather than hardcoding views in the factory method.
Alternatively, I tried to add a faces message to the message list, and issue a redirect upon detection of the error. This works, to the extent that messages are added and the page is redirected, but it has its problems...
In both cases, the factory method is called multiple times in the original page since when the factory method is called, an error occurs and the variable is never resolved. Therefore, we repeat the factory code with the error in it multiple times.
The second method has the unpleasant side effect that the faces message is added multiple times as opposed to just being added once and re-directed. Regarding the first method, it still works, but again, it's just wasteful to keep repeating the factory method when it won't work. Being an exception should surely force Seam to stop whatever it is doing and work based on the exception.
In a worst case scenario, I could use the custom exception classes, and redirect to specific pages per entity type as needed, or I could use a flag to indicate whether an error has already occured in the bean to prevent multiple messages. However, again, it seems to come down to a best practices question.
I was just wondering if anyone had any experiences with good entity exception handling? The problem however, could also extend to other types of exceptions such as authorization exceptions when you determine that the user cannot perform specific functions.