Skip to content

Commit

Permalink
#53: Add logs to diagnose the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrondeau committed Nov 13, 2017
1 parent c9b25df commit e191050
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions GoToWindow/ExitCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum ExitCodes
PluginDirectoryNotFound = 120,
NoPluginsFound = 121,
ErrorLoadingPlugins = 122,
ErrorLoadingPluginsTypes = 123,
UnhandledError = 500
}
}
52 changes: 43 additions & 9 deletions GoToWindow/GoToWindowPluginsContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,68 @@ public static GoToWindowPluginsContainer LoadPlugins()
var pluginsContainer = new GoToWindowPluginsContainer();
container.ComposeParts(pluginsContainer);

DiagnosticCatalogComposition(catalog);

if(pluginsContainer.Plugins == null || !pluginsContainer.Plugins.Any())
throw new InstanceNotFoundException("No plug-ins found");
{
HandleError(new Exception("No plugins were composed"), ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow.");
return null;
}

pluginsContainer.Plugins = pluginsContainer.Plugins.OrderBy(plugin => plugin.Sequence).ToList();
return pluginsContainer;
}
catch (InstanceNotFoundException exc)
{
HandleError(exc, ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow.");
HandleError(exc, ExitCodes.NoPluginsFound, "No plug-ins found. Check that at least GoToWindow.Plugins.Core.dll can be found in the Plugins directory and restart GoToWindow.");
return null;
}
catch(ReflectionTypeLoadException exc)
{
HandleError(exc, ExitCodes.ErrorLoadingPluginsTypes, "An error occured while loading plugin types.", string.Join("; ", exc.LoaderExceptions.Select(e => e.Message)));
return null;
}
catch (Exception exc)
catch (Exception exc)
{
HandleError(exc, ExitCodes.ErrorLoadingPlugins, "An error occured while loading plug-ins. Try updating or removing plugins other than GoToWindow.Plugins.Core.dll from the Plugins directory and restart GoToWindow.");
return null;
}
}

private static void HandleError(Exception exc, ExitCodes exitCode, string message)
{
var typeLoadExc = exc as ReflectionTypeLoadException;
private static void DiagnosticCatalogComposition(AggregateCatalog catalog)
{
if (!Log.IsDebugEnabled) return;

if (typeLoadExc != null)
Log.Error(string.Join("; ", typeLoadExc.LoaderExceptions.Select(e => e.Message)), typeLoadExc);
try
{
foreach (var c in catalog.Catalogs)
{
if (c is DirectoryCatalog directoryCatalog)
{
Log.Debug($"Loaded directory catalog from '{directoryCatalog.FullPath}', looking for '{directoryCatalog.SearchPattern}'. Loaded: {string.Join(", ", directoryCatalog.LoadedFiles)}");
}
else
{
Log.Debug($"Loaded catalog of type {c.GetType().FullName}");
}
}
}
catch (Exception exc)
{
Log.Warn("Failed generating catalog information", exc);
}
}

private static void HandleError(Exception exc, ExitCodes exitCode, string message, string additionalInfo = null)
{
if (additionalInfo != null)
Log.Error(additionalInfo, exc);
else
Log.Error(exc);



MessageBox.Show(message, "Startup Error", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show(message, "Startup Error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown((int)exitCode);
}
}
Expand Down

0 comments on commit e191050

Please sign in to comment.