2

I have a CustomExceptionHandler from which I want to display a modal dialog to get some information from the user, using Primefaces dialog framework.

The CustomExceptionHandler#handle method is being called as expected when I deliberately cause a Null Pointer exception. And I can display and interact with the dialog's .xhtml page if I directly invoke it from a p:commandButton's actionListener on a xhtml page (both cases with ajax enabled and disabled).

When I invoke a p:commandButton (with ajax disabled to eliminate a possible ajax error from complicating the scenario) on a page that leads to the NPE, both before/after log messages are generated from CustomExceptionHandler#handle, but the dialog page is not displayed. The window flashes like it's being refreshed, however.

From CustomExceptoinHandler.java:

public class CustomExceptionHandler
      extends ExceptionHandlerWrapper
{

  private ExceptionHandler wrapped;

  public CustomExceptionHandler(ExceptionHandler wrapped)
  {
    this.wrapped = wrapped;
  }

  @Override
  public ExceptionHandler getWrapped()
  {
    return wrapped;
  }

  @Override
  public void handle()
        throws FacesException
  {
    Iterator<ExceptionQueuedEvent> iterator = getUnhandledExceptionQueuedEvents().iterator();
    while (iterator.hasNext()) {
      boolean removeError = false;
      Throwable t = iterator.next().getContext().getException();
      while (t.getCause() != null)
        t = t.getCause();
      if (t instanceof NullPointerException) {
        Logging.devLogger.warn("NPE exception handler before dialog");
        Map<String, Object> options = new HashMap<>();
        options.put("modal", true);
        String page = "/home/dialogs/exceptionDialog.xhtml";
        RequestContext.getCurrentInstance().openDialog(page, options, null);
        Logging.devLogger.warn("NPE exception handler after dialog");
        removeError = true;
      } // TBD: other cases and refactoring ....
      if (removeError) iterator.remove();
    }
    getWrapped().handle();
  }
}

The RequestContext....openDialog() invocation is the same when handling the exception and when the dialog is directly opened via a commandButton. I've determined that the backing bean for the exception dialog (view scoped) gets constructed when the dialog is opened outside the exception situation, but does not get constructed when opened while handling the exception (@PostConstruct method not called).

How would I change the way the exception is handled so the dialog behaves as expected? I'm using Mojarra 2.2.5, Primefaces 4.0, Glassfish 4.0.

CraftWeaver
  • 707
  • 1
  • 8
  • 21

1 Answers1

0

I'm woring with PrimeFaces 5.2, i've solved this problem in other way.

To show the dialog, i've created the error dialog in my template named "errorDialog" and te show it by my CustomExceptionHandler i've used, this command

RequestContext.getCurrentInstance().execute("PF('errorDialog').show();");

You can't use the command below, becose there is already an exception, so you can rendered the dialog.

RequestContext.getCurrentInstance().openDialog(page, options, null);

Concerning the exception handling by @PostConstruct method, i've used :

@PostConstruct
    private void init() {
        try {
            initController();
        } catch (Exception ex) {
            logger.error(ex, null);                             
            //Show the error dialog
            MyUtil.openErrorDialog();
        }
    }

Note : I have still a problem, when an exception (Error 500) is occurred and handled by the App Server and not my CustomHandlerClass, i can't show the dialog becose there is already a redirection by the App Server.

bilelovitch
  • 1,975
  • 1
  • 16
  • 24