New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSF - ExceptionMapper #395

Closed
wodarski opened this Issue Aug 2, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@wodarski

wodarski commented Aug 2, 2017

Hello,

I would like to see in Omnifaces (or maybe in JSF spec) a cleaner way to convert an exception to a FacesMessage. Today I have to handle exceptions manually and convert them to FacesMessage like this:

@Named
@RequestedScoped
public class Foo {
  @Inject
  SomeEJB ejb;
  public void execute() {
    try {
      ejb.doSomething();
    } catch (MyException e) {
      Messages.addGlobalError(e.getMessage());
      return;
    }
  }
}

The problem with this aproach is that I need to handle the exception manually and convert it to FacesMessage all over the place, leading to a lot of boilerplate code.

I really would like to see something similar to JAX-RS ExceptionMapper [1] in Omnifaces/JSF. Pseudo-code example:

@FacesMessageMapper
public class MyExceptionMapper implements ExceptionMapper<MyException> {
  @Override
  public FacesMessage toFacesMessage(MyException e) {
    return Messages.addGlobalError(e.getMessage());
  }
}

I know I could achieve almost the same result with a JSF Exception Handler, but I can only register one Exception Handler per application, and I want to use Omnifaces FullAjaxExceptionHandler (which in my opinion should be standardized in JSF by the way, but that is another discussion :)).

What do you guys think?

Regards.

[1] https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ExceptionMapper.html

@kukel

This comment has been minimized.

Show comment
Hide comment
@kukel

kukel Aug 2, 2017

Untill this is implemented, you can easily do this with interceptors... I use this all the time.

See: https://stackoverflow.com/questions/30976334/handle-exception-thrown-from-a-cdi-interceptor-as-a-faces-message

kukel commented Aug 2, 2017

Untill this is implemented, you can easily do this with interceptors... I use this all the time.

See: https://stackoverflow.com/questions/30976334/handle-exception-thrown-from-a-cdi-interceptor-as-a-faces-message

@wodarski

This comment has been minimized.

Show comment
Hide comment
@wodarski

wodarski Aug 2, 2017

Nice suggestion @kukel.

I usually tend to think on Interceptors to use at EJB/Business layer, but I definitely could use it for this scenario.

wodarski commented Aug 2, 2017

Nice suggestion @kukel.

I usually tend to think on Interceptors to use at EJB/Business layer, but I definitely could use it for this scenario.

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Aug 2, 2017

Member

You can extend FullAjaxExceptionHandler as explained here https://stackoverflow.com/q/34144536/157882

Member

BalusC commented Aug 2, 2017

You can extend FullAjaxExceptionHandler as explained here https://stackoverflow.com/q/34144536/157882

@wodarski

This comment has been minimized.

Show comment
Hide comment
@wodarski

wodarski Aug 3, 2017

@BalusC Extending FullAjaxExceptionHandler won't work for non-ajax requests. :(

wodarski commented Aug 3, 2017

@BalusC Extending FullAjaxExceptionHandler won't work for non-ajax requests. :(

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC Sep 17, 2017

Member

@BalusC Extending FullAjaxExceptionHandler won't work for non-ajax requests. :(

Right, I have adjusted that for 2.6.5. Let me know if it works for you with today's latest 2.6.5-SNAPSHOT.

Member

BalusC commented Sep 17, 2017

@BalusC Extending FullAjaxExceptionHandler won't work for non-ajax requests. :(

Right, I have adjusted that for 2.6.5. Let me know if it works for you with today's latest 2.6.5-SNAPSHOT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment