IException, GenericException

Adds ability to wrap an existing exception and push it through the pipe
This commit is contained in:
Justin Swanson 2019-12-04 22:57:05 -06:00
parent adbddfa16e
commit 2c47f54752
5 changed files with 49 additions and 14 deletions

View File

@ -6,10 +6,11 @@ using System.Threading.Tasks;
namespace Wabbajack.Common.StatusFeed
{
public abstract class AErrorMessage : Exception, IError
public abstract class AErrorMessage : Exception, IException
{
public DateTime Timestamp { get; } = DateTime.Now;
public abstract string ShortDescription { get; }
public abstract string ExtendedDescription { get; }
Exception IException.Exception => this;
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack.Common.StatusFeed.Errors
{
public class GenericException : IException
{
public string ExtraMessage { get; }
public DateTime Timestamp { get; } = DateTime.Now;
public string ShortDescription => ExtraMessage ?? Exception?.Message;
public string ExtendedDescription => $"{ExtraMessage}: {Exception?.ToString()}";
public Exception Exception { get; }
public GenericException(Exception exception, string extraMessage = null)
{
ExtraMessage = extraMessage;
Exception = exception;
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wabbajack.Common.StatusFeed
{
public interface IException : IError
{
Exception Exception { get; }
}
}

View File

@ -63,31 +63,23 @@ namespace Wabbajack.Common
public static T Log<T>(T msg) where T : IStatusMessage
{
lock (_lock)
{
File.AppendAllText(LogFile, msg + "\r\n");
}
LogToFile(msg.ShortDescription);
LoggerSubj.OnNext(msg);
return msg;
}
public static void Error(AErrorMessage err)
public static void Error(IException err)
{
lock (_lock)
{
File.AppendAllText(LogFile, err.ShortDescription + "\r\n");
}
LogToFile(err.ShortDescription);
LoggerSubj.OnNext(err);
throw err;
throw err.Exception;
}
public static void LogToFile(string msg)
{
lock (_lock)
{
msg = $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}";
File.AppendAllText(LogFile, msg + "\r\n");
File.AppendAllText(LogFile, $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}\r\n");
}
}

View File

@ -112,9 +112,11 @@
<Compile Include="StatusFeed\AStatusMessage.cs" />
<Compile Include="StatusFeed\Errors\7zipReturnError.cs" />
<Compile Include="StatusFeed\Errors\FileExtractionError.cs" />
<Compile Include="StatusFeed\Errors\GenericException.cs" />
<Compile Include="StatusFeed\Errors\UnconvertedError.cs" />
<Compile Include="StatusFeed\GenericInfo.cs" />
<Compile Include="StatusFeed\IError.cs" />
<Compile Include="StatusFeed\IException.cs" />
<Compile Include="StatusFeed\IInfo.cs" />
<Compile Include="StatusFeed\IStatusMessage.cs" />
<Compile Include="StatusFeed\IUserIntervention.cs" />