1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-25 08:58:49 +00:00

refactor!: change exception thrown by GetHash and TryWriteHash

The methods no longer throw TypeInitializationException, and instead now throw ArgumentException.
This commit is contained in:
Oliver Booth 2023-08-23 14:17:42 +01:00
parent 5c21c86a52
commit 0bf89bb82a
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
3 changed files with 11 additions and 14 deletions

View File

@ -36,6 +36,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: DateTime.Age(DateTime) and DateTimeOffset.Age(DateTimeOffset) parameter renamed from asOf to referenceDate.
- X10D: Methods which accepted the `Endianness` enum as an argument have been replaced with explicit
BigEndian/LittleEndian methods.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of
TypeInitializationException.
### Removed

View File

@ -86,16 +86,15 @@ internal partial class StreamTests
[Test]
public void NullCreateMethodShouldThrow()
{
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>());
Assert.Throws<TypeInitializationException>(() =>
Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>());
Assert.Throws<ArgumentException>(() => Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
}
[Test]
public void NoCreateMethodShouldThrow()
{
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>());
Assert.Throws<TypeInitializationException>(() =>
Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>());
Assert.Throws<ArgumentException>(() =>
Stream.Null.TryWriteHash<HashAlgorithmTestClassNoCreateMethod>(Span<byte>.Empty, out _));
}

View File

@ -1,4 +1,4 @@
using System.Buffers.Binary;
using System.Buffers.Binary;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
@ -45,15 +45,13 @@ public static class StreamExtensions
.FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0);
if (createMethod is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
throw new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod);
}
using var crypt = createMethod.Invoke(null, null) as T;
if (crypt is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull));
throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull);
}
return crypt.ComputeHash(stream);
@ -617,15 +615,13 @@ public static class StreamExtensions
.FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0);
if (createMethod is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
throw new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod);
}
using var crypt = createMethod.Invoke(null, null) as T;
if (crypt is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull));
throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull);
}
if (stream.Length > int.MaxValue)