Skip to content

Commit

Permalink
Merge pull request #86 from patriksvensson/bug/GH-66
Browse files Browse the repository at this point in the history
Handle exceptions better
  • Loading branch information
patriksvensson authored Nov 7, 2018
2 parents d5085ca + fc1d6b9 commit c3bd651
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/Jarvis.Addin.Files/Indexing/FileIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal sealed class FileIndexer : IBackgroundWorker, IFileIndex, IHandle<Trigg

public FileIndexer(IEventAggregator events, IEnumerable<IFileIndexSource> sources, IJarvisLog log)
{
_log = new LogDecorator("FileIndexer", log);
_log = new LogDecorator(nameof(FileIndexer), log);
_sources = new List<IFileIndexSource>(sources ?? Array.Empty<IFileIndexSource>());
_stopWords = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "to", "the" };
_comparer = new ScoreComparer();
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis.Addin.Files/Sources/DocumentIndexSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexedEntry> Index()
Expand Down
29 changes: 22 additions & 7 deletions src/Jarvis.Addin.Files/Sources/StartMenuIndexSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IndexedEntry> 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);
Expand All @@ -54,6 +54,21 @@ public IEnumerable<IndexedEntry> Index()
}
}

private IEnumerable<IFile> 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<IFile>();
}
}

private static IWshShortcut GetShortcut(IFile file)
{
try
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis.Addin.Files/Sources/Uwp/AppxManifestReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions src/Jarvis.Addin.Files/Sources/UwpIndexSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexedEntry> Index()
Expand All @@ -56,7 +56,7 @@ public IEnumerable<IndexedEntry> Index()
}
}

return result;
return result;
}

private bool TryGetIndexedEntry(Package package, out IndexedEntry[] entry)
Expand All @@ -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;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis.Addin.Google/GoogleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal sealed class GoogleProvider : QueryProvider<GoogleResult>

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"));
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis.Addin.Wikipedia/WikipediaProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal sealed class WikipediaProvider : QueryProvider<WikipediaResult>

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"));
}
Expand Down
9 changes: 9 additions & 0 deletions src/Jarvis.Core/Threading/TaskWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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})");
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis.Package/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp rescap desktop">

<Identity Name="SpectreSystemsAB.Jarvis-Yourroboticbutler" Publisher="CN=BD765503-99E8-40AA-91EC-B3E295890BB4" Version="0.9.10014.0" />
<Identity Name="SpectreSystemsAB.Jarvis-Yourroboticbutler" Publisher="CN=BD765503-99E8-40AA-91EC-B3E295890BB4" Version="0.9.10016.0" />
<Properties>
<DisplayName>Jarvis - Your robotic butler</DisplayName>
<PublisherDisplayName>Spectre Systems AB</PublisherDisplayName>
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Jarvis.Core;
using Module = Autofac.Module;

namespace Jarvis.Bootstrapping
namespace Jarvis.Infrastructure.Bootstrapping
{
public sealed class AddinModule : Module
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using Spectre.System.IO;
#endif

namespace Jarvis.Bootstrapping
namespace Jarvis.Infrastructure.Bootstrapping
{
public sealed class LoggingModule : Module
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using Jarvis.Core;

namespace Jarvis.Bootstrapping.Seeding
namespace Jarvis.Infrastructure.Bootstrapping.Seeding
{
public sealed class GeneralSettingsSeeder : ISettingsSeeder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Autofac;
using Jarvis.Services.Updating;

namespace Jarvis.Bootstrapping
namespace Jarvis.Infrastructure.Bootstrapping
{
public sealed class UpdaterModule : Module
{
Expand Down
10 changes: 5 additions & 5 deletions src/Jarvis/Jarvis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Bootstrapper.cs" />
<Compile Include="Bootstrapping\AddinModule.cs" />
<Compile Include="Bootstrapping\Seeding\GeneralSettingsSeeder.cs" />
<Compile Include="Bootstrapping\UpdaterModule.cs" />
<Compile Include="Infrastructure\Bootstrapping\AddinModule.cs" />
<Compile Include="Infrastructure\Bootstrapping\Seeding\GeneralSettingsSeeder.cs" />
<Compile Include="Infrastructure\Bootstrapping\UpdaterModule.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Infrastructure\Utilities\KeyboardHook.cs" />
<Compile Include="Infrastructure\Presentation\Controls\BindableRichTextBox.cs" />
Expand All @@ -137,8 +137,8 @@
</Compile>
<Compile Include="Messages\ExitMessage.cs" />
<Compile Include="Messages\UpdateAvailableMessage.cs" />
<Compile Include="Bootstrapping\JarvisModule.cs" />
<Compile Include="Bootstrapping\LoggingModule.cs" />
<Compile Include="Infrastructure\Bootstrapping\JarvisModule.cs" />
<Compile Include="Infrastructure\Bootstrapping\LoggingModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
13 changes: 10 additions & 3 deletions src/Jarvis/JarvisApplication.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis/Services/ServiceOrchestrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public sealed class ServiceOrchestrator

public ServiceOrchestrator(IEnumerable<IBackgroundWorker> workers, IJarvisLog log)
{
_log = new LogDecorator("ServiceOrchestrator", log);
_log = new LogDecorator(nameof(ServiceOrchestrator), log);
_workers = new List<IBackgroundWorker>(workers);
_tasks = new List<Task>();
_stopped = new ManualResetEvent(true);
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis/Services/SettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public SettingsService(IFileSystem fileSystem, IEnumerable<ISettingsSeeder> seed
{
_fileSystem = fileSystem;
_seeders = new List<ISettingsSeeder>(seeders);
_log = log;
_log = new LogDecorator(nameof(SettingsService), log);
_lock = new object();
_settings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_dirty = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis/Services/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Jarvis/Services/Updating/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c3bd651

Please sign in to comment.