1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 16:35:42 +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: 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 - X10D: Methods which accepted the `Endianness` enum as an argument have been replaced with explicit
BigEndian/LittleEndian methods. BigEndian/LittleEndian methods.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of
TypeInitializationException.
### Removed ### Removed

View File

@ -86,16 +86,15 @@ internal partial class StreamTests
[Test] [Test]
public void NullCreateMethodShouldThrow() public void NullCreateMethodShouldThrow()
{ {
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>()); Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>());
Assert.Throws<TypeInitializationException>(() => Assert.Throws<ArgumentException>(() => Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
} }
[Test] [Test]
public void NoCreateMethodShouldThrow() public void NoCreateMethodShouldThrow()
{ {
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>()); Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>());
Assert.Throws<TypeInitializationException>(() => Assert.Throws<ArgumentException>(() =>
Stream.Null.TryWriteHash<HashAlgorithmTestClassNoCreateMethod>(Span<byte>.Empty, out _)); 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.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security.Cryptography; using System.Security.Cryptography;
@ -45,15 +45,13 @@ public static class StreamExtensions
.FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0); .FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0);
if (createMethod is null) if (createMethod is null)
{ {
throw new TypeInitializationException(type.FullName, throw new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod);
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
} }
using var crypt = createMethod.Invoke(null, null) as T; using var crypt = createMethod.Invoke(null, null) as T;
if (crypt is null) if (crypt is null)
{ {
throw new TypeInitializationException(type.FullName, throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull);
new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull));
} }
return crypt.ComputeHash(stream); return crypt.ComputeHash(stream);
@ -617,15 +615,13 @@ public static class StreamExtensions
.FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0); .FirstOrDefault(c => c.Name == "Create" && c.GetParameters().Length == 0);
if (createMethod is null) if (createMethod is null)
{ {
throw new TypeInitializationException(type.FullName, throw new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod);
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
} }
using var crypt = createMethod.Invoke(null, null) as T; using var crypt = createMethod.Invoke(null, null) as T;
if (crypt is null) if (crypt is null)
{ {
throw new TypeInitializationException(type.FullName, throw new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull);
new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull));
} }
if (stream.Length > int.MaxValue) if (stream.Length > int.MaxValue)