Skip to content

Commit

Permalink
use WarningsService from Boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
lostmsu committed Jan 20, 2020
1 parent 0a9f2a5 commit 6031ccb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
52 changes: 18 additions & 34 deletions Warning.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
namespace LostTech.Stack.Utils
namespace LostTech.Stack.WindowManagement
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.HockeyApp;
using LostTech.App;

public static class WarningExtensions
{
public static void ReportAsWarning(this Exception exception, string prefix = "Warning: ") {
if (exception != null) {
string message = prefix + exception.Message;
HockeyClient.Current.TrackTrace(message, SeverityLevel.Warning, properties: new SortedDictionary<string, string> {
[nameof(exception.StackTrace)] = exception.StackTrace,
[nameof(exception.Source)] = exception.Source,
[nameof(exception.HResult)] = exception.HResult.ToString(CultureInfo.InvariantCulture),
[nameof(exception.InnerException)] = exception.InnerException?.ToString(),
});
}
}
static IWarningsService? warningsService;

public static void ReportAsWarning([NotNull] this Task<Exception> potentiallyFailingTask, string prefix = "Warning: ") {
if (potentiallyFailingTask == null)
throw new ArgumentNullException(nameof(potentiallyFailingTask));
potentiallyFailingTask.ContinueWith(t => {
if (t.IsFaulted)
t.Exception.ReportAsWarning(prefix);
if (t.IsCompleted)
t.Result.ReportAsWarning(prefix);
});
public static IWarningsService WarningsService {
get => warningsService ?? throw new InvalidOperationException($"{nameof(WarningsService)} is not initialized.");
set {
if (value is null)
throw new ArgumentNullException(nameof(WarningsService));
if (Interlocked.CompareExchange(ref warningsService, value, null) != null)
throw new InvalidOperationException();
}
}

public static void ReportAsWarning([NotNull] this Task potentiallyFailingTask, string prefix = "Warning: ")
{
if (potentiallyFailingTask == null)
throw new ArgumentNullException(nameof(potentiallyFailingTask));
potentiallyFailingTask.ContinueWith(t => {
if (t.IsFaulted)
t.Exception.ReportAsWarning(prefix);
});
}
public static void ReportAsWarning(this Exception exception, string prefix = "Warning: ")
=> WarningsService.ReportAsWarning(exception, prefix);
public static void ReportAsWarning(this Task<Exception> potentiallyFailingTask, string prefix = "Warning: ")
=> WarningsService.ReportAsWarning(potentiallyFailingTask, prefix);
public static void ReportAsWarning(this Task potentiallyFailingTask, string prefix = "Warning: ")
=> WarningsService.ReportAsWarning(potentiallyFailingTask, prefix);
}
}
5 changes: 3 additions & 2 deletions WindowManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>LostTech.Stack.WindowManagement</AssemblyName>
<RootNamespace>LostTech.Stack.WindowManagement</RootNamespace>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<Deterministic>False</Deterministic>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HockeySDK.Community.Core" Version="4.1.7-pre0" />
<PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="LostTech.App.Boilerplate" Version="0.1.0" />
</ItemGroup>

</Project>

0 comments on commit 6031ccb

Please sign in to comment.