From fc1d6b9051cb056b70d6fc56b881812fd0ba6194 Mon Sep 17 00:00:00 2001 From: Patrik Svensson Date: Wed, 7 Nov 2018 18:30:55 +0100 Subject: [PATCH] Handle exceptions better Also cleaned up diagnostics a bit. Closes #66 Closes #73 Closes #74 --- .../Indexing/FileIndexer.cs | 2 +- .../Sources/DocumentIndexSource.cs | 2 +- .../Sources/StartMenuIndexSource.cs | 29 +++++++++++++----- .../Sources/Uwp/AppxManifestReader.cs | 2 +- .../Sources/UwpIndexSource.cs | 30 ++++++++++--------- src/Jarvis.Addin.Google/GoogleProvider.cs | 2 +- .../WikipediaProvider.cs | 2 +- src/Jarvis.Core/Threading/TaskWrapper.cs | 9 ++++++ src/Jarvis.Package/Package.appxmanifest | 2 +- src/Jarvis/Bootstrapper.cs | 2 +- .../Bootstrapping/AddinModule.cs | 2 +- .../Bootstrapping/JarvisModule.cs | 5 ++-- .../Bootstrapping/LoggingModule.cs | 2 +- .../Seeding/GeneralSettingsSeeder.cs | 2 +- .../Bootstrapping/UpdaterModule.cs | 2 +- src/Jarvis/Jarvis.csproj | 10 +++---- src/Jarvis/JarvisApplication.xaml.cs | 13 ++++++-- src/Jarvis/Services/ServiceOrchestrator.cs | 2 +- src/Jarvis/Services/SettingsService.cs | 2 +- src/Jarvis/Services/UpdateService.cs | 2 +- src/Jarvis/Services/Updating/UpdateChecker.cs | 2 +- 21 files changed, 79 insertions(+), 47 deletions(-) rename src/Jarvis/{ => Infrastructure}/Bootstrapping/AddinModule.cs (98%) rename src/Jarvis/{ => Infrastructure}/Bootstrapping/JarvisModule.cs (95%) rename src/Jarvis/{ => Infrastructure}/Bootstrapping/LoggingModule.cs (96%) rename src/Jarvis/{ => Infrastructure}/Bootstrapping/Seeding/GeneralSettingsSeeder.cs (93%) rename src/Jarvis/{ => Infrastructure}/Bootstrapping/UpdaterModule.cs (94%) diff --git a/src/Jarvis.Addin.Files/Indexing/FileIndexer.cs b/src/Jarvis.Addin.Files/Indexing/FileIndexer.cs index 2701012..9737585 100644 --- a/src/Jarvis.Addin.Files/Indexing/FileIndexer.cs +++ b/src/Jarvis.Addin.Files/Indexing/FileIndexer.cs @@ -35,7 +35,7 @@ internal sealed class FileIndexer : IBackgroundWorker, IFileIndex, IHandle sources, IJarvisLog log) { - _log = new LogDecorator("FileIndexer", log); + _log = new LogDecorator(nameof(FileIndexer), log); _sources = new List(sources ?? Array.Empty()); _stopWords = new HashSet(StringComparer.OrdinalIgnoreCase) { "to", "the" }; _comparer = new ScoreComparer(); diff --git a/src/Jarvis.Addin.Files/Sources/DocumentIndexSource.cs b/src/Jarvis.Addin.Files/Sources/DocumentIndexSource.cs index 2906a48..709c17b 100644 --- a/src/Jarvis.Addin.Files/Sources/DocumentIndexSource.cs +++ b/src/Jarvis.Addin.Files/Sources/DocumentIndexSource.cs @@ -29,7 +29,7 @@ public DocumentIndexSource(IFileSystem fileSystem, ISettingsStore settings, IJar { _fileSystem = fileSystem; _settings = settings; - _log = log; + _log = new LogDecorator(nameof(DocumentIndexSource), log); } public IEnumerable Index() diff --git a/src/Jarvis.Addin.Files/Sources/StartMenuIndexSource.cs b/src/Jarvis.Addin.Files/Sources/StartMenuIndexSource.cs index 760530a..e0486a9 100644 --- a/src/Jarvis.Addin.Files/Sources/StartMenuIndexSource.cs +++ b/src/Jarvis.Addin.Files/Sources/StartMenuIndexSource.cs @@ -9,6 +9,7 @@ using IWshRuntimeLibrary; using Jarvis.Addin.Files.Extensions; using Jarvis.Addin.Files.Indexing; +using Jarvis.Core.Diagnostics; using JetBrains.Annotations; using Spectre.System.IO; using IFile = Spectre.System.IO.IFile; @@ -20,23 +21,22 @@ namespace Jarvis.Addin.Files.Sources internal sealed class StartMenuIndexSource : IFileIndexSource { private readonly IFileSystem _fileSystem; + private readonly IJarvisLog _log; public string Name => "Start menu"; - public StartMenuIndexSource(IFileSystem fileSystem) + public StartMenuIndexSource(IFileSystem fileSystem, IJarvisLog log) { _fileSystem = fileSystem; + _log = new LogDecorator(nameof(StartMenuIndexSource), log); } public IEnumerable Index() { - var common = _fileSystem.GetDirectory(new DirectoryPath(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu))); - var roaming = _fileSystem.GetDirectory(new DirectoryPath(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu))); - var files = common.GetFiles("*.lnk", SearchScope.Recursive) - .Concat(common.GetFiles("*.url", SearchScope.Recursive)) - .Concat(roaming.GetFiles("*.lnk", SearchScope.Recursive)) - .Concat(roaming.GetFiles("*.url", SearchScope.Recursive)); + var common = GetFilesInSpecialFolder(Environment.SpecialFolder.CommonStartMenu); + var roaming = GetFilesInSpecialFolder(Environment.SpecialFolder.StartMenu); + var files = common.Concat(roaming); foreach (var file in files) { var shortcut = GetShortcut(file); @@ -54,6 +54,21 @@ public IEnumerable Index() } } + private IEnumerable GetFilesInSpecialFolder(Environment.SpecialFolder folder) + { + try + { + var directory = _fileSystem.GetDirectory(new DirectoryPath(Environment.GetFolderPath(folder))); + return directory.GetFiles("*.lnk", SearchScope.Recursive) + .Concat(directory.GetFiles("*.url", SearchScope.Recursive)); + } + catch (Exception ex) + { + _log.Error($"An error occured while indexing '{folder}': {ex.Message}"); + return Enumerable.Empty(); + } + } + private static IWshShortcut GetShortcut(IFile file) { try diff --git a/src/Jarvis.Addin.Files/Sources/Uwp/AppxManifestReader.cs b/src/Jarvis.Addin.Files/Sources/Uwp/AppxManifestReader.cs index c292c59..787acca 100644 --- a/src/Jarvis.Addin.Files/Sources/Uwp/AppxManifestReader.cs +++ b/src/Jarvis.Addin.Files/Sources/Uwp/AppxManifestReader.cs @@ -21,7 +21,7 @@ public AppxManifestReader(IFileSystem fileSystem, INativeStreamProvider streamPr { _fileSystem = fileSystem; _streamProvider = streamProvider; - _log = log; + _log = new LogDecorator(nameof(AppxManifestReader), _log); } public AppxManifest Read(FilePath path) diff --git a/src/Jarvis.Addin.Files/Sources/UwpIndexSource.cs b/src/Jarvis.Addin.Files/Sources/UwpIndexSource.cs index 5641c82..4f376e1 100644 --- a/src/Jarvis.Addin.Files/Sources/UwpIndexSource.cs +++ b/src/Jarvis.Addin.Files/Sources/UwpIndexSource.cs @@ -31,7 +31,7 @@ public UwpIndexSource(AppxManifestReader reader, IFileSystem fileSystem, IJarvis { _reader = reader; _fileSystem = fileSystem; - _log = log; + _log = new LogDecorator(nameof(UwpIndexSource), log); } public IEnumerable Index() @@ -56,7 +56,7 @@ public IEnumerable Index() } } - return result; + return result; } private bool TryGetIndexedEntry(Package package, out IndexedEntry[] entry) @@ -67,21 +67,23 @@ private bool TryGetIndexedEntry(Package package, out IndexedEntry[] entry) if (_fileSystem.File.Exists(file)) { var manifest = _reader.Read(file); - - entry = new IndexedEntry[manifest.Applications.Count]; - for (var index = 0; index < manifest.Applications.Count; index++) + if (manifest != null) { - var app = manifest.Applications[index]; + entry = new IndexedEntry[manifest.Applications.Count]; + for (var index = 0; index < manifest.Applications.Count; index++) + { + var app = manifest.Applications[index]; - entry[index] = new UwpIndexSourceEntry( - $"{manifest.Id}.{app.Id}", - app.AppUserModelId, - app.DisplayName, - GetIcon(manifest, app), - manifest.Description ?? manifest.Publisher); - } + entry[index] = new UwpIndexSourceEntry( + $"{manifest.Id}.{app.Id}", + app.AppUserModelId, + app.DisplayName, + GetIcon(manifest, app), + manifest.Description ?? manifest.Publisher); + } - return true; + return true; + } } } diff --git a/src/Jarvis.Addin.Google/GoogleProvider.cs b/src/Jarvis.Addin.Google/GoogleProvider.cs index 827808f..f0dae95 100644 --- a/src/Jarvis.Addin.Google/GoogleProvider.cs +++ b/src/Jarvis.Addin.Google/GoogleProvider.cs @@ -30,7 +30,7 @@ internal sealed class GoogleProvider : QueryProvider public GoogleProvider(IJarvisLog log) { - _log = log; + _log = new LogDecorator(nameof(GoogleProvider), log); _client = new HttpClient(); _googleIcon = new BitmapImage(new Uri("pack://application:,,,/Jarvis.Addin.Google;component/Resources/Google.png")); _linkIcon = new BitmapImage(new Uri("pack://application:,,,/Jarvis.Addin.Google;component/Resources/Link.png")); diff --git a/src/Jarvis.Addin.Wikipedia/WikipediaProvider.cs b/src/Jarvis.Addin.Wikipedia/WikipediaProvider.cs index ac7b9bf..953c568 100644 --- a/src/Jarvis.Addin.Wikipedia/WikipediaProvider.cs +++ b/src/Jarvis.Addin.Wikipedia/WikipediaProvider.cs @@ -31,7 +31,7 @@ internal sealed class WikipediaProvider : QueryProvider public WikipediaProvider(IJarvisLog log) { - _log = log; + _log = new LogDecorator(nameof(WikipediaProvider), log); _client = new HttpClient(); _icon = new BitmapImage(new Uri("pack://application:,,,/Jarvis.Addin.Wikipedia;component/Resources/Wikipedia.png")); } diff --git a/src/Jarvis.Core/Threading/TaskWrapper.cs b/src/Jarvis.Core/Threading/TaskWrapper.cs index f3035da..675fdea 100644 --- a/src/Jarvis.Core/Threading/TaskWrapper.cs +++ b/src/Jarvis.Core/Threading/TaskWrapper.cs @@ -44,6 +44,15 @@ public Task Start(CancellationTokenSource source) { _log.Information($"{Name} aborted."); } + catch (AggregateException ex) + { + var exceptions = ex.Flatten().InnerExceptions; + foreach (var exception in exceptions) + { + _log.Error($"{Name}: {ex.Message} ({ex.GetType().FullName})"); + } + _source.Cancel(); + } catch (Exception ex) { _log.Error($"{Name}: {ex.Message} ({ex.GetType().FullName})"); diff --git a/src/Jarvis.Package/Package.appxmanifest b/src/Jarvis.Package/Package.appxmanifest index 09268b3..3842adf 100644 --- a/src/Jarvis.Package/Package.appxmanifest +++ b/src/Jarvis.Package/Package.appxmanifest @@ -1,7 +1,7 @@  - + Jarvis - Your robotic butler Spectre Systems AB diff --git a/src/Jarvis/Bootstrapper.cs b/src/Jarvis/Bootstrapper.cs index c482b54..075893a 100644 --- a/src/Jarvis/Bootstrapper.cs +++ b/src/Jarvis/Bootstrapper.cs @@ -10,8 +10,8 @@ using Jarvis.Addin.Files; using Jarvis.Addin.Google; using Jarvis.Addin.Wikipedia; -using Jarvis.Bootstrapping; using Jarvis.Core; +using Jarvis.Infrastructure.Bootstrapping; using Jarvis.Infrastructure.Utilities; using Jarvis.Services; using Jarvis.ViewModels; diff --git a/src/Jarvis/Bootstrapping/AddinModule.cs b/src/Jarvis/Infrastructure/Bootstrapping/AddinModule.cs similarity index 98% rename from src/Jarvis/Bootstrapping/AddinModule.cs rename to src/Jarvis/Infrastructure/Bootstrapping/AddinModule.cs index 74954b8..a893c38 100644 --- a/src/Jarvis/Bootstrapping/AddinModule.cs +++ b/src/Jarvis/Infrastructure/Bootstrapping/AddinModule.cs @@ -12,7 +12,7 @@ using Jarvis.Core; using Module = Autofac.Module; -namespace Jarvis.Bootstrapping +namespace Jarvis.Infrastructure.Bootstrapping { public sealed class AddinModule : Module { diff --git a/src/Jarvis/Bootstrapping/JarvisModule.cs b/src/Jarvis/Infrastructure/Bootstrapping/JarvisModule.cs similarity index 95% rename from src/Jarvis/Bootstrapping/JarvisModule.cs rename to src/Jarvis/Infrastructure/Bootstrapping/JarvisModule.cs index 33babf7..b290553 100644 --- a/src/Jarvis/Bootstrapping/JarvisModule.cs +++ b/src/Jarvis/Infrastructure/Bootstrapping/JarvisModule.cs @@ -6,16 +6,15 @@ using System.ComponentModel; using Autofac; using Caliburn.Micro; -using Jarvis.Bootstrapping.Seeding; using Jarvis.Core; using Jarvis.Core.Threading; -using Jarvis.Infrastructure.Utilities; +using Jarvis.Infrastructure.Bootstrapping.Seeding; using Jarvis.Services; using Jarvis.ViewModels; using Spectre.System.IO; using Module = Autofac.Module; -namespace Jarvis.Bootstrapping +namespace Jarvis.Infrastructure.Bootstrapping { public sealed class JarvisModule : Module { diff --git a/src/Jarvis/Bootstrapping/LoggingModule.cs b/src/Jarvis/Infrastructure/Bootstrapping/LoggingModule.cs similarity index 96% rename from src/Jarvis/Bootstrapping/LoggingModule.cs rename to src/Jarvis/Infrastructure/Bootstrapping/LoggingModule.cs index 4e9eafa..c74452c 100644 --- a/src/Jarvis/Bootstrapping/LoggingModule.cs +++ b/src/Jarvis/Infrastructure/Bootstrapping/LoggingModule.cs @@ -12,7 +12,7 @@ using Spectre.System.IO; #endif -namespace Jarvis.Bootstrapping +namespace Jarvis.Infrastructure.Bootstrapping { public sealed class LoggingModule : Module { diff --git a/src/Jarvis/Bootstrapping/Seeding/GeneralSettingsSeeder.cs b/src/Jarvis/Infrastructure/Bootstrapping/Seeding/GeneralSettingsSeeder.cs similarity index 93% rename from src/Jarvis/Bootstrapping/Seeding/GeneralSettingsSeeder.cs rename to src/Jarvis/Infrastructure/Bootstrapping/Seeding/GeneralSettingsSeeder.cs index 1544950..90db100 100644 --- a/src/Jarvis/Bootstrapping/Seeding/GeneralSettingsSeeder.cs +++ b/src/Jarvis/Infrastructure/Bootstrapping/Seeding/GeneralSettingsSeeder.cs @@ -4,7 +4,7 @@ using Jarvis.Core; -namespace Jarvis.Bootstrapping.Seeding +namespace Jarvis.Infrastructure.Bootstrapping.Seeding { public sealed class GeneralSettingsSeeder : ISettingsSeeder { diff --git a/src/Jarvis/Bootstrapping/UpdaterModule.cs b/src/Jarvis/Infrastructure/Bootstrapping/UpdaterModule.cs similarity index 94% rename from src/Jarvis/Bootstrapping/UpdaterModule.cs rename to src/Jarvis/Infrastructure/Bootstrapping/UpdaterModule.cs index 5570215..4a1bde1 100644 --- a/src/Jarvis/Bootstrapping/UpdaterModule.cs +++ b/src/Jarvis/Infrastructure/Bootstrapping/UpdaterModule.cs @@ -5,7 +5,7 @@ using Autofac; using Jarvis.Services.Updating; -namespace Jarvis.Bootstrapping +namespace Jarvis.Infrastructure.Bootstrapping { public sealed class UpdaterModule : Module { diff --git a/src/Jarvis/Jarvis.csproj b/src/Jarvis/Jarvis.csproj index 83f7e0a..a6088ec 100644 --- a/src/Jarvis/Jarvis.csproj +++ b/src/Jarvis/Jarvis.csproj @@ -116,9 +116,9 @@ Properties\SharedAssemblyInfo.cs - - - + + + @@ -137,8 +137,8 @@ - - + + Code diff --git a/src/Jarvis/JarvisApplication.xaml.cs b/src/Jarvis/JarvisApplication.xaml.cs index 8e40d27..13c656a 100644 --- a/src/Jarvis/JarvisApplication.xaml.cs +++ b/src/Jarvis/JarvisApplication.xaml.cs @@ -22,9 +22,16 @@ private void Dispose(bool disposing) { if (disposing && _mutex != null) { - _mutex.ReleaseMutex(); - _mutex.Close(); - _mutex = null; + try + { + _mutex.ReleaseMutex(); + _mutex.Close(); + _mutex = null; + } + catch + { + // Nothing we can do about this. + } } } diff --git a/src/Jarvis/Services/ServiceOrchestrator.cs b/src/Jarvis/Services/ServiceOrchestrator.cs index fa73c65..7b1aaf3 100644 --- a/src/Jarvis/Services/ServiceOrchestrator.cs +++ b/src/Jarvis/Services/ServiceOrchestrator.cs @@ -23,7 +23,7 @@ public sealed class ServiceOrchestrator public ServiceOrchestrator(IEnumerable workers, IJarvisLog log) { - _log = new LogDecorator("ServiceOrchestrator", log); + _log = new LogDecorator(nameof(ServiceOrchestrator), log); _workers = new List(workers); _tasks = new List(); _stopped = new ManualResetEvent(true); diff --git a/src/Jarvis/Services/SettingsService.cs b/src/Jarvis/Services/SettingsService.cs index 781e62a..2662207 100644 --- a/src/Jarvis/Services/SettingsService.cs +++ b/src/Jarvis/Services/SettingsService.cs @@ -25,7 +25,7 @@ public SettingsService(IFileSystem fileSystem, IEnumerable seed { _fileSystem = fileSystem; _seeders = new List(seeders); - _log = log; + _log = new LogDecorator(nameof(SettingsService), log); _lock = new object(); _settings = new Dictionary(StringComparer.OrdinalIgnoreCase); _dirty = false; diff --git a/src/Jarvis/Services/UpdateService.cs b/src/Jarvis/Services/UpdateService.cs index c3b9439..d1cc2a4 100644 --- a/src/Jarvis/Services/UpdateService.cs +++ b/src/Jarvis/Services/UpdateService.cs @@ -31,7 +31,7 @@ public UpdateService(IUpdateChecker checker, ISettingsStore settings, _settings = settings; _eventAggregator = eventAggregator; _application = application; - _log = new LogDecorator("UpdateService", log); + _log = new LogDecorator(nameof(UpdateService), log); } public bool Enabled() diff --git a/src/Jarvis/Services/Updating/UpdateChecker.cs b/src/Jarvis/Services/Updating/UpdateChecker.cs index 87fda5b..b1f57ad 100644 --- a/src/Jarvis/Services/Updating/UpdateChecker.cs +++ b/src/Jarvis/Services/Updating/UpdateChecker.cs @@ -28,7 +28,7 @@ public sealed class UpdateChecker : IUpdateChecker public UpdateChecker(ISettingsStore settings, IJarvisLog log) { _settings = settings; - _log = log; + _log = new LogDecorator(nameof(UpdateChecker), log); _currentVersion = new SemVersion(typeof(UpdateService).Assembly.GetName().Version); _client = new HttpClient();