From a58e5e8c407f7982203f24892a04baf448d9d4b6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 30 Apr 2023 22:35:34 +0200 Subject: [PATCH] Add messages to all InvalidOperationException-s --- .../InvalidOperationExceptionExtensions.cs | 28 ++++++++++++++++++- .../CanvasEffect.EffectGraph.cs | 8 +++--- .../CanvasEffect.Interop.cs | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs index 38a6cd11..c5f082c6 100644 --- a/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs +++ b/src/ComputeSharp.Core/Polyfills/InvalidOperationExceptionExtensions.cs @@ -36,7 +36,22 @@ internal static class InvalidOperationExceptionExtensions } /// - /// Throws an for a given parameter name. + /// Throws an if is . + /// + /// Dummy value to invoke the extension upon (always pass . + /// The condition to decide whether to throw the exception. + /// The message to include in the exception. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ThrowIf(this InvalidOperationException? _, [DoesNotReturnIf(true)] bool condition, string message) + { + if (condition) + { + Throw(message); + } + } + + /// + /// Throws an . /// /// Always thrown with no parameters. [DoesNotReturn] @@ -44,4 +59,15 @@ internal static class InvalidOperationExceptionExtensions { throw new InvalidOperationException(); } + + /// + /// Throws an with a given message. + /// + /// The message to include in the exception. + /// Always thrown with no parameters. + [DoesNotReturn] + private static void Throw(string message) + { + throw new InvalidOperationException(message); + } } diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs index b3beefbf..1be88a83 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.EffectGraph.cs @@ -80,7 +80,7 @@ partial class CanvasEffect { default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation."); // Use a new dummy object as key to register the anonymous effect node. This is cheaper than having to maintain a different // data structure (eg. a HashSet instance) just to hold the anonymous effects that may be registered. This way, @@ -109,7 +109,7 @@ partial class CanvasEffect default(ArgumentNullException).ThrowIfNull(effectNode); default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation."); // Try to add the new canvas image associated with the input effect node marker. // This must not have been previously added from another RegisterNode call. @@ -138,7 +138,7 @@ partial class CanvasEffect { default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation."); // Register the anonymous effect node with a dummy object, same as above _ = this.owner.transformNodes.TryAdd(new object(), canvasImage); @@ -158,7 +158,7 @@ partial class CanvasEffect default(ArgumentNullException).ThrowIfNull(effectNode); default(ArgumentNullException).ThrowIfNull(canvasImage); default(InvalidOperationException).ThrowIf(this.owner is null); - default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation); + default(InvalidOperationException).ThrowIf(this.owner.invalidationType != InvalidationType.Creation, "The effect graph cannot be modified after its creation."); // Try to add the output node as in the method above (but with an extra check before doing so) if (!this.owner.transformNodes.TryAdd(effectNode, canvasImage)) diff --git a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs index 88ef69c6..29bc06f4 100644 --- a/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs +++ b/src/ComputeSharp.D2D1.UI/CanvasEffect.Interop.cs @@ -97,7 +97,7 @@ unsafe partial class CanvasEffect // At this point, there must be an output canvas image being set. // If not, it means a derived type has forgot to set an output node. - default(InvalidOperationException).ThrowIf(this.canvasImage is null); + default(InvalidOperationException).ThrowIf(this.canvasImage is null, "No output node is set in the effect graph."); return this.canvasImage; }