Rethrow exceptions with external result code

This commit is contained in:
Alex Barney 2019-07-01 19:32:38 -05:00
parent 69658ae08e
commit 6adcc8cce0
3 changed files with 164 additions and 21 deletions

View File

@ -144,72 +144,184 @@ namespace LibHac.Fs.Save
public void CreateDirectory(string path)
{
SaveDataFileSystemCore.CreateDirectory(path);
try
{
SaveDataFileSystemCore.CreateDirectory(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void CreateFile(string path, long size, CreateFileOptions options)
{
SaveDataFileSystemCore.CreateFile(path, size, options);
try
{
SaveDataFileSystemCore.CreateFile(path, size, options);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void DeleteDirectory(string path)
{
SaveDataFileSystemCore.DeleteDirectory(path);
try
{
SaveDataFileSystemCore.DeleteDirectory(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void DeleteDirectoryRecursively(string path)
{
SaveDataFileSystemCore.DeleteDirectoryRecursively(path);
try
{
SaveDataFileSystemCore.DeleteDirectoryRecursively(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void CleanDirectoryRecursively(string path)
{
SaveDataFileSystemCore.CleanDirectoryRecursively(path);
try
{
SaveDataFileSystemCore.CleanDirectoryRecursively(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void DeleteFile(string path)
{
SaveDataFileSystemCore.DeleteFile(path);
try
{
SaveDataFileSystemCore.DeleteFile(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public IDirectory OpenDirectory(string path, OpenDirectoryMode mode)
{
return SaveDataFileSystemCore.OpenDirectory(path, mode);
try
{
return SaveDataFileSystemCore.OpenDirectory(path, mode);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public IFile OpenFile(string path, OpenMode mode)
{
return SaveDataFileSystemCore.OpenFile(path, mode);
try
{
return SaveDataFileSystemCore.OpenFile(path, mode);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void RenameDirectory(string srcPath, string dstPath)
{
SaveDataFileSystemCore.RenameDirectory(srcPath, dstPath);
try
{
SaveDataFileSystemCore.RenameDirectory(srcPath, dstPath);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void RenameFile(string srcPath, string dstPath)
{
SaveDataFileSystemCore.RenameFile(srcPath, dstPath);
try
{
SaveDataFileSystemCore.RenameFile(srcPath, dstPath);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public DirectoryEntryType GetEntryType(string path)
{
return SaveDataFileSystemCore.GetEntryType(path);
try
{
return SaveDataFileSystemCore.GetEntryType(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public long GetFreeSpaceSize(string path)
{
return SaveDataFileSystemCore.GetFreeSpaceSize(path);
try
{
return SaveDataFileSystemCore.GetFreeSpaceSize(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public long GetTotalSpaceSize(string path)
{
return SaveDataFileSystemCore.GetTotalSpaceSize(path);
try
{
return SaveDataFileSystemCore.GetTotalSpaceSize(path);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public void Commit()
{
Commit(Keyset);
try
{
Commit(Keyset);
}
catch (HorizonResultException ex)
{
ConvertResultException(ex);
throw;
}
}
public FileTimeStampRaw GetFileTimeStampRaw(string path)
@ -283,5 +395,10 @@ namespace LibHac.Fs.Save
return journalValidity;
}
private void ConvertResultException(HorizonResultException ex)
{
ex.ResultValue = SaveResults.ConvertToExternalResult(ex.ResultValue);
}
}
}

View File

@ -9,7 +9,14 @@ namespace LibHac
/// <summary>
/// The result code of the error.
/// </summary>
public Result ResultValue { get; }
public Result ResultValue { get; set; }
/// <summary>
/// The original, internal result code if it was converted to a more general external result code.
/// </summary>
public Result InternalResultValue { get; }
public string InnerMessage { get; }
/// <summary>
/// Initializes a new instance of the <see cref="HorizonResultException"/> class.
@ -17,6 +24,7 @@ namespace LibHac
/// <param name="result">The result code for the reason for the exception.</param>
public HorizonResultException(Result result)
{
InternalResultValue = result;
ResultValue = result;
}
@ -26,9 +34,10 @@ namespace LibHac
/// <param name="result">The result code for the reason for the exception.</param>
/// <param name="message">The error message that explains the reason for the exception.</param>
public HorizonResultException(Result result, string message)
: base(message)
{
InternalResultValue = result;
ResultValue = result;
InnerMessage = message;
}
/// <summary>
@ -39,9 +48,11 @@ namespace LibHac
/// <param name="message">The error message that explains the reason for the exception.</param>
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
public HorizonResultException(Result result, string message, Exception innerException)
: base(message, innerException)
: base(string.Empty, innerException)
{
InternalResultValue = result;
ResultValue = result;
InnerMessage = message;
}
/// <summary>
@ -52,24 +63,26 @@ namespace LibHac
protected HorizonResultException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
InternalResultValue = (Result)info.GetValue(nameof(InternalResultValue), InternalResultValue.GetType());
ResultValue = (Result)info.GetValue(nameof(ResultValue), ResultValue.GetType());
InnerMessage = (string)info.GetValue(nameof(InnerMessage), InnerMessage.GetType());
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(InternalResultValue), InternalResultValue);
info.AddValue(nameof(ResultValue), ResultValue);
info.AddValue(nameof(InnerMessage), InnerMessage);
}
public override string Message
{
get
{
string baseMessage = base.Message;
if (!string.IsNullOrWhiteSpace(baseMessage))
if (!string.IsNullOrWhiteSpace(InnerMessage))
{
return $"{ResultValue.ErrorCode}: {baseMessage}";
return $"{ResultValue.ErrorCode}: {InnerMessage}";
}
return ResultValue.ErrorCode;

View File

@ -18,6 +18,19 @@ namespace hactoolnet
string name = ex.Type == KeyType.Title ? $"Title key for rights ID {ex.Name}" : ex.Name;
Console.Error.WriteLine($"\nERROR: {ex.Message}\nA required key is missing.\nKey name: {name}\n");
}
catch (HorizonResultException ex)
{
Console.Error.WriteLine($"\nERROR: {ex.Message}");
if (ex.ResultValue != ex.InternalResultValue)
{
Console.Error.WriteLine($"Internal Code: {ex.InternalResultValue.ErrorCode}");
}
Console.Error.WriteLine();
Console.Error.WriteLine("Additional information:");
Console.Error.WriteLine(ex.StackTrace);
}
catch (Exception ex)
{
Console.Error.WriteLine($"\nERROR: {ex.Message}\n");