From 0bf89bb82a881dbb38c837e408d40ee46baace22 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 23 Aug 2023 14:17:42 +0100 Subject: [PATCH] refactor!: change exception thrown by GetHash and TryWriteHash The methods no longer throw TypeInitializationException, and instead now throw ArgumentException. --- CHANGELOG.md | 2 ++ X10D.Tests/src/IO/StreamTests.cs | 9 ++++----- X10D/src/IO/StreamExtensions.cs | 14 +++++--------- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1957cf2..5f31c88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/X10D.Tests/src/IO/StreamTests.cs b/X10D.Tests/src/IO/StreamTests.cs index e240990..b522265 100644 --- a/X10D.Tests/src/IO/StreamTests.cs +++ b/X10D.Tests/src/IO/StreamTests.cs @@ -86,16 +86,15 @@ internal partial class StreamTests [Test] public void NullCreateMethodShouldThrow() { - Assert.Throws(() => Stream.Null.GetHash()); - Assert.Throws(() => - Stream.Null.TryWriteHash(Span.Empty, out _)); + Assert.Throws(() => Stream.Null.GetHash()); + Assert.Throws(() => Stream.Null.TryWriteHash(Span.Empty, out _)); } [Test] public void NoCreateMethodShouldThrow() { - Assert.Throws(() => Stream.Null.GetHash()); - Assert.Throws(() => + Assert.Throws(() => Stream.Null.GetHash()); + Assert.Throws(() => Stream.Null.TryWriteHash(Span.Empty, out _)); } diff --git a/X10D/src/IO/StreamExtensions.cs b/X10D/src/IO/StreamExtensions.cs index 9bc7243..ceb34c8 100644 --- a/X10D/src/IO/StreamExtensions.cs +++ b/X10D/src/IO/StreamExtensions.cs @@ -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)