Search code examples
javaeclipsercpe4

Eclipse E4 RCP global error handling


My problem is the same as the one in this thread: "I want to set a custom error handler, which is called each time an unexpected error occurs. How can I achieve that?"

How can I do this in E4? In E3 it can be achieved by overriding the eventLoopException method of the ApplicationWorkbenchAdvisor

The thread links to another thread where a StatusReporter is being extended. Now I am able to extend StatusReporter but its methods are not invoked automatically when some unexpected exception occurs. Also I get a discouraged access warning that StatusReporter is not an API.

Another approach, as mentioned in the wiki, is to use StatusHandler. But I do not want to invoke the StatusHandler manually. I want a global handler which automatically picks up un-handled exceptions...

Any help would be greatly appreciated.

Thanks, Jehan


Solution

  • You can put your own implementation of IEventLoopAdvisor in to the context. The @PostContextCreate method of your LifeCycle class is a good place to add this.

    IEventLoopAdvisor has two methods:

    @Override
    public void eventLoopIdle(final Display display)
    {
      // Called whenever the RCP is idle, you can do other things here
      // but it is very important to call 'display.sleep()'
    
      display.sleep();
    }
    
    @Override
    public void eventLoopException(final Throwable exception)
    {
      // Add code for unhandled exceptions
    }
    

    Note: IEventLoopHandler is an internal class so normally it should be avoided but this usage seems to be sanctioned.

    If you want to override the logging of all messages you can create your own class derived from org.eclipse.e4.core.services.log.Logger and inject that in to the Eclipse context.