Page 1 of 1

Unhandled Exceptions in a Plugin

PostPosted: Thu Dec 13, 2018 5:07 pm
by SBarnes
Is there a way either with some plugin code or setting to get Jiwa to log any unhandled exceptions in a plugin to say the application log rather than just getting the message box of the error?

Re: Unhandled Exceptions in a Plugin

PostPosted: Thu Dec 13, 2018 5:13 pm
by Mike.Sheen
Hi Stuart,

Maybe... We display those messageboxes by adding a handler for the UnhandledException and ThreadException in the Jiwa.Exe form. In theory a plugin could do the same.

The adding of the handlers:
Code: Select all
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
AddHandler System.Windows.Forms.Application.ThreadException, AddressOf UnhandledFormExceptionHandler


The handlers themselves:
Code: Select all
Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
   If TypeOf e.ExceptionObject IsNot JiwaApplication.Exceptions.ClientCancelledException Then
      If DirectCast(e.ExceptionObject, System.Exception).Data.Contains("Custom Message") Then
         ReportError(DirectCast(e.ExceptionObject, System.Exception).Data("Custom Message"), DirectCast(e.ExceptionObject, System.Exception).GetBaseException.TargetSite.Name)
      Else
         ReportError(DirectCast(e.ExceptionObject, System.Exception).GetBaseException.Message, DirectCast(e.ExceptionObject, System.Exception).GetBaseException.TargetSite.Name)
      End If

   End If
End Sub

Private Sub UnhandledFormExceptionHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
   If TypeOf e.Exception IsNot JiwaApplication.Exceptions.ClientCancelledException Then
      If e.Exception.Data.Contains("Custom Message") Then
         ReportError(e.Exception.Data("Custom Message"), e.Exception.GetBaseException.TargetSite.Name)
      Else
         ReportError(e.Exception.GetBaseException.Message, e.Exception.GetBaseException.TargetSite.Name)
      End If
   End If
End Sub


Have fun!

Re: Unhandled Exceptions in a Plugin

PostPosted: Fri Dec 14, 2018 7:11 am
by SBarnes
Hi Mike,

I had a stab at this, all the events hook up ok but nothing fires and Jiwa just still reports the error as normal, any ideas?

Re: Unhandled Exceptions in a Plugin

PostPosted: Fri Dec 14, 2018 5:02 pm
by Mike.Sheen
Hi Stuart,

I moved these 3 lines in your plugin into the Setup method of the ApplicationManagerPlugin and your handlers are being invoked upon exception:
Code: Select all
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += PluginUnhandledExceptionHandler;
System.Windows.Forms.Application.ThreadException += PluginUnhandledFormExceptionHandler;


Plugin attached is yours with the above change.

Mike

Re: Unhandled Exceptions in a Plugin

PostPosted: Fri Dec 14, 2018 5:30 pm
by SBarnes
Hi Mike,

I loaded your version in and it still won't work, where were you throwing the exception from I have been trying to do it from another plugin, even tried changing the executing order so the catcher was called first?