0

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

Jehan Zeb
  • 155
  • 12

1 Answers1

1

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Thanks for your quick reply. I implemented the `IEventLoopAdvisor` and put it in context as you suggested. Its method eventLoopIdle() is called by the framework, but unfortunately the eventLoopException() method is not called on unhandled exceptions... Do I need to do you something extra for that to be called? – Jehan Zeb Oct 26 '15 at 10:32
  • No. If the eventLoopIdle is being called this is working. Perhaps the exceptions you want to deal with are actually being handled somewhere. If the message you get does not say 'Internal Error xxxxx' it is not coming from the default eventLoopException. – greg-449 Oct 26 '15 at 10:47
  • I am throwing a dummy `IllegalArgumentException` from the `@Persist` method of my `Part`, just to test this. Perhaps its not a valid test case then... How can I test if it is working? – Jehan Zeb Oct 26 '15 at 10:52
  • Errors caused by methods invoked by injection are logged separately. You can handle those by injecting your own `Logger` class. I'm not sure of a way to test the eventLoopException - I run on Mac OS X 10.11 which currently causes regular unexpected exceptions so it is easy to test! – greg-449 Oct 26 '15 at 11:09
  • Thanks for your help but I don't want to handle unhandled exceptions everywhere and log them. I want to log them at a central place... Somehow the framework catches them and logs them on the console. I want to catch them and write them to my log file. – Jehan Zeb Oct 26 '15 at 14:28
  • 1
    There is no one method for that anymore, you need to use a replacement `Logger` - mentioned that in the answer. – greg-449 Oct 26 '15 at 14:42