2
votes

I am developing an application in JSF 2 and I've got a question about the best practices.

What is the best practice for treating JSF message, I must treat the business Validation error messages via try/catch in managed Beans/CDI Bean or the best practice would be to treat all screen errors exceptions within a ExceptionHandlerWrapper?

Example:

public String doCreate() {

    try {
        //...SomeCode
        addInformationMessage(MESSAGE_SUCCESS_DATA_INSERTED);
    } catch (BusinessValidationException bve) {
        addErrorMessage(bve);
        return Outcome.FAILURE; 
    } catch (WebValidationException wve) {
        addErrorMessage(wve);
        return Outcome.FAILURE;     
    } catch (BusinessException e) {
        throw new WebException(e);
    }
    return Outcome.SUCCESS;
}

Or in a custom ExceptionHandlerWrapper:

public class AppExceptionHandler extends ExceptionHandlerWrapper {
// my implemantation
}

I researched a lot about this subject, but found nothing very clear, what is the best way to treat known error exceptions?

Thank for attention

1
Yes, it's a simple question, but I have this dilemma, I must deal with the exceptions business of my first example of the way and leave only for ExceptionHandlerWrapper structural exceptions like (ConversationExpiredException, SessionExpiredException, ViewExpiredException, NonexistentConversationException, SecurityException, ... ) or I must prepare my ExceptionHandlerWrapper for any type of exception that I wanted to treat in business and leave my cleaner bean possible? - Estevão Jordão

1 Answers

1
votes

The best practice is using the right way.

The right way in this particular case is the way without code duplication.

Copypasting/repeating the same piece of code over all backing bean methods is code duplication. It's therefore not the right way and inherently thus also not the best practice.

This is by the way regardless of the problem you're trying to solve.

See also: