diff --git a/src/LibHac/Fs/Save/SaveDataFileSystem.cs b/src/LibHac/Fs/Save/SaveDataFileSystem.cs
index 110ad6a3..8191ccba 100644
--- a/src/LibHac/Fs/Save/SaveDataFileSystem.cs
+++ b/src/LibHac/Fs/Save/SaveDataFileSystem.cs
@@ -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);
+ }
}
}
diff --git a/src/LibHac/HorizonResultException.cs b/src/LibHac/HorizonResultException.cs
index 136e7709..3b075898 100644
--- a/src/LibHac/HorizonResultException.cs
+++ b/src/LibHac/HorizonResultException.cs
@@ -9,7 +9,14 @@ namespace LibHac
///
/// The result code of the error.
///
- public Result ResultValue { get; }
+ public Result ResultValue { get; set; }
+
+ ///
+ /// The original, internal result code if it was converted to a more general external result code.
+ ///
+ public Result InternalResultValue { get; }
+
+ public string InnerMessage { get; }
///
/// Initializes a new instance of the class.
@@ -17,6 +24,7 @@ namespace LibHac
/// The result code for the reason for the exception.
public HorizonResultException(Result result)
{
+ InternalResultValue = result;
ResultValue = result;
}
@@ -26,9 +34,10 @@ namespace LibHac
/// The result code for the reason for the exception.
/// The error message that explains the reason for the exception.
public HorizonResultException(Result result, string message)
- : base(message)
{
+ InternalResultValue = result;
ResultValue = result;
+ InnerMessage = message;
}
///
@@ -39,9 +48,11 @@ namespace LibHac
/// The error message that explains the reason for the exception.
/// The exception that is the cause of the current exception, or a null reference if no inner exception is specified.
public HorizonResultException(Result result, string message, Exception innerException)
- : base(message, innerException)
+ : base(string.Empty, innerException)
{
+ InternalResultValue = result;
ResultValue = result;
+ InnerMessage = message;
}
///
@@ -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;
diff --git a/src/hactoolnet/Program.cs b/src/hactoolnet/Program.cs
index 531fb176..9f39a649 100644
--- a/src/hactoolnet/Program.cs
+++ b/src/hactoolnet/Program.cs
@@ -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");