Add HorizonResultException

This commit is contained in:
Alex Barney 2019-06-09 11:25:00 -05:00
parent fc149bf4c4
commit f45d4166eb
5 changed files with 106 additions and 1 deletions

View file

@ -0,0 +1,11 @@
namespace LibHac.Fs
{
public class ResultsFs
{
public const int ModuleFs = 2;
public static Result ResultFsMountNameAlreadyExists => new Result(ModuleFs, 60);
public static Result ResultFsWritableFileOpen => new Result(ModuleFs, 6457);
public static Result ResultFsMountNameNotFound => new Result(ModuleFs, 6905);
}
}

View file

@ -0,0 +1,79 @@
using System;
using System.Runtime.Serialization;
namespace LibHac
{
[Serializable]
public class HorizonResultException : LibHacException, ISerializable
{
/// <summary>
/// The result code of the error.
/// </summary>
public Result ResultValue { get; }
/// <summary>
/// Initializes a new instance of the <see cref="HorizonResultException"/> class.
/// </summary>
/// <param name="result">The result code for the reason for the exception.</param>
public HorizonResultException(Result result)
{
ResultValue = result;
}
/// <summary>
/// Initializes a new instance of the <see cref="HorizonResultException"/> class with a specified error message.
/// </summary>
/// <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)
{
ResultValue = result;
}
/// <summary>
/// Initializes a new instance of the <see cref="HorizonResultException"/> class with a specified error message
/// and a reference to the inner exception that is the cause of this exception.
/// </summary>
/// <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>
/// <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)
{
ResultValue = result;
}
/// <summary>
/// Initializes a new instance of the <see cref="MissingKeyException"/> class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected HorizonResultException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
ResultValue = (Result)info.GetValue(nameof(ResultValue), ResultValue.GetType());
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(ResultValue), ResultValue);
}
public override string Message
{
get
{
string baseMessage = base.Message;
if (!string.IsNullOrWhiteSpace(baseMessage))
{
return $"{ResultValue.ErrorCode}: {baseMessage}";
}
return ResultValue.ErrorCode;
}
}
}
}

View file

@ -68,12 +68,15 @@ namespace LibHac
protected MissingKeyException(SerializationInfo info, StreamingContext context) protected MissingKeyException(SerializationInfo info, StreamingContext context)
: base(info, context) : base(info, context)
{ {
Name = info.GetString(nameof(Name));
Type = (KeyType)info.GetValue(nameof(Type), Type.GetType());
} }
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{ {
base.GetObjectData(info, context); base.GetObjectData(info, context);
info.AddValue(nameof(Name), Name); info.AddValue(nameof(Name), Name);
info.AddValue(nameof(Type), Type);
} }
public override string Message public override string Message

View file

@ -1,5 +1,8 @@
namespace LibHac using System;
namespace LibHac
{ {
[Serializable]
public struct Result public struct Result
{ {
public readonly int Value; public readonly int Value;
@ -16,6 +19,7 @@
public int Description => (Value >> 9) & 0x1FFF; public int Description => (Value >> 9) & 0x1FFF;
public int Module => Value & 0x1FF; public int Module => Value & 0x1FF;
public string ErrorCode => $"{2000 + Module:d4}-{Description:d4}";
public bool IsSuccess() => Value == 0; public bool IsSuccess() => Value == 0;
public bool IsFailure() => Value != 0; public bool IsFailure() => Value != 0;

View file

@ -0,0 +1,8 @@
namespace LibHac
{
internal static class ThrowHelper
{
public static void ThrowResult(Result result) => throw new HorizonResultException(result);
public static void ThrowResult(Result result, string message) => throw new HorizonResultException(result, message);
}
}