Skip to content

Commit cdb0112

Browse files
authored
Revisit error messages for readability (#429)
Introduce new helpers to provide better error messages and reduce code, compatibly with .NET Standard 2.0 Use new code in the Abstractions package. Fix some exception types uses in Abstractions.
1 parent bcf07e4 commit cdb0112

11 files changed

Lines changed: 347 additions & 61 deletions

service/Abstractions/AppBuilders/ServiceCollectionPool.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.KernelMemory.Diagnostics;
89

910
namespace Microsoft.KernelMemory.AppBuilders;
1011

@@ -50,11 +51,7 @@ public class ServiceCollectionPool : IServiceCollection
5051
/// <param name="primaryCollection">The primary service collection</param>
5152
public ServiceCollectionPool(IServiceCollection primaryCollection)
5253
{
53-
if (primaryCollection == null)
54-
{
55-
throw new ArgumentNullException(nameof(primaryCollection), "The primary service collection cannot be NULL");
56-
}
57-
54+
ArgumentNullExceptionEx.ThrowIfNull(primaryCollection, nameof(primaryCollection), "The primary service collection cannot be NULL");
5855
this._poolSizeLocked = false;
5956
this._primaryCollection = primaryCollection;
6057
this._pool = new List<IServiceCollection> { primaryCollection };

service/Abstractions/Configuration/TextPartitioningOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,27 @@ public void Validate()
3232
{
3333
if (this.MaxTokensPerParagraph < 1)
3434
{
35-
throw new ConfigurationException("The number of tokens per paragraph cannot be less than 1");
35+
throw new ConfigurationException($"Text partitioning: {nameof(this.MaxTokensPerParagraph)} cannot be less than 1");
3636
}
3737

3838
if (this.MaxTokensPerLine < 1)
3939
{
40-
throw new ConfigurationException("The number of tokens per line cannot be less than 1");
40+
throw new ConfigurationException($"Text partitioning: {nameof(this.MaxTokensPerLine)} cannot be less than 1");
4141
}
4242

4343
if (this.OverlappingTokens < 0)
4444
{
45-
throw new ConfigurationException("The number of overlapping tokens cannot be less than 0");
45+
throw new ConfigurationException($"Text partitioning: {nameof(this.OverlappingTokens)} cannot be less than 0");
4646
}
4747

4848
if (this.MaxTokensPerLine > this.MaxTokensPerParagraph)
4949
{
50-
throw new ConfigurationException("The number of tokens per line cannot be more than the tokens per paragraph");
50+
throw new ConfigurationException($"Text partitioning: {nameof(this.MaxTokensPerLine)} cannot be more than {nameof(this.MaxTokensPerParagraph)}");
5151
}
5252

5353
if (this.OverlappingTokens >= this.MaxTokensPerParagraph)
5454
{
55-
throw new ConfigurationException("The number of overlapping tokens must be less than the tokens per paragraph");
55+
throw new ConfigurationException($"Text partitioning: {nameof(this.OverlappingTokens)} must be less than {nameof(this.MaxTokensPerParagraph)}");
5656
}
5757
}
5858
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
namespace Microsoft.KernelMemory.Diagnostics;
8+
9+
#pragma warning disable CS8777
10+
public static class ArgumentNullExceptionEx
11+
{
12+
public static void ThrowIfNull([NotNull] object? argument, string? paramName = null, string message = "")
13+
{
14+
if (argument != null) { return; }
15+
16+
throw new ArgumentNullException(paramName, message);
17+
}
18+
19+
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, string? paramName = null, string message = "")
20+
{
21+
if (!string.IsNullOrWhiteSpace(argument)) { return; }
22+
23+
throw new ArgumentNullException(paramName, message);
24+
}
25+
26+
public static void ThrowIfNullOrEmpty([NotNull] string? argument, string? paramName = null, string message = "")
27+
{
28+
if (!string.IsNullOrEmpty(argument)) { return; }
29+
30+
throw new ArgumentNullException(paramName, message);
31+
}
32+
33+
public static void ThrowIfNotNullOrEmpty([NotNull] string? argument, string? paramName = null, string message = "")
34+
{
35+
if (string.IsNullOrEmpty(argument)) { return; }
36+
37+
throw new ArgumentNullException(paramName, message);
38+
}
39+
40+
public static void ThrowIfEmpty<T>([NotNull] IList<T>? argument, string? paramName = null, string message = "")
41+
{
42+
if (argument is { Count: > 0 }) { return; }
43+
44+
throw new ArgumentNullException(paramName, message);
45+
}
46+
}
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.KernelMemory.Diagnostics;
7+
8+
public static class ArgumentOutOfRangeExceptionEx
9+
{
10+
// ======== Generics ========
11+
12+
public static void ThrowIfEqual<T>(T value, T other, string? paramName, string message)
13+
{
14+
if (!EqualityComparer<T>.Default.Equals(value, other)) { return; }
15+
16+
throw new ArgumentOutOfRangeException(paramName, message);
17+
}
18+
19+
public static void ThrowIfNotEqual<T>(T value, T other, string? paramName, string message)
20+
{
21+
if (EqualityComparer<T>.Default.Equals(value, other)) { return; }
22+
23+
throw new ArgumentOutOfRangeException(paramName, message);
24+
}
25+
26+
public static void ThrowIf(bool condition, string? paramName, string message)
27+
{
28+
if (!condition) { return; }
29+
30+
throw new ArgumentOutOfRangeException(paramName, message);
31+
}
32+
33+
public static void ThrowIfNot(bool condition, string? paramName, string message)
34+
{
35+
if (condition) { return; }
36+
37+
throw new ArgumentOutOfRangeException(paramName, message);
38+
}
39+
40+
// ======== int ========
41+
42+
public static void ThrowIfZero(int value, string paramName, string message)
43+
{
44+
if (value != 0) { return; }
45+
46+
throw new ArgumentOutOfRangeException(paramName, message);
47+
}
48+
49+
public static void ThrowIfNegative(int value, string paramName, string message)
50+
{
51+
if (value >= 0) { return; }
52+
53+
throw new ArgumentOutOfRangeException(paramName, message);
54+
}
55+
56+
public static void ThrowIfZeroOrNegative(int value, string paramName, string message)
57+
{
58+
if (value > 0) { return; }
59+
60+
throw new ArgumentOutOfRangeException(paramName, message);
61+
}
62+
63+
public static void ThrowIfLessThan(int value, int other, string paramName, string message)
64+
{
65+
if (value >= other) { return; }
66+
67+
throw new ArgumentOutOfRangeException(paramName, message);
68+
}
69+
70+
public static void ThrowIfEqualToOrLessThan(int value, int other, string paramName, string message)
71+
{
72+
if (value > other) { return; }
73+
74+
throw new ArgumentOutOfRangeException(paramName, message);
75+
}
76+
77+
public static void ThrowIfGreaterThan(int value, int other, string paramName, string message)
78+
{
79+
if (value <= other) { return; }
80+
81+
throw new ArgumentOutOfRangeException(paramName, message);
82+
}
83+
84+
public static void ThrowIfEqualToOrGreaterThan(int value, int other, string paramName, string message)
85+
{
86+
if (value < other) { return; }
87+
88+
throw new ArgumentOutOfRangeException(paramName, message);
89+
}
90+
91+
// ======== uint ========
92+
93+
public static void ThrowIfZero(uint value, string paramName, string message)
94+
{
95+
if (value != 0) { return; }
96+
97+
throw new ArgumentOutOfRangeException(paramName, message);
98+
}
99+
100+
public static void ThrowIfNegative(uint value, string paramName, string message)
101+
{
102+
if (value >= 0) { return; }
103+
104+
throw new ArgumentOutOfRangeException(paramName, message);
105+
}
106+
107+
public static void ThrowIfZeroOrNegative(uint value, string paramName, string message)
108+
{
109+
if (value > 0) { return; }
110+
111+
throw new ArgumentOutOfRangeException(paramName, message);
112+
}
113+
114+
public static void ThrowIfLessThan(uint value, uint other, string paramName, string message)
115+
{
116+
if (value >= other) { return; }
117+
118+
throw new ArgumentOutOfRangeException(paramName, message);
119+
}
120+
121+
public static void ThrowIfEqualToOrLessThan(uint value, uint other, string paramName, string message)
122+
{
123+
if (value > other) { return; }
124+
125+
throw new ArgumentOutOfRangeException(paramName, message);
126+
}
127+
128+
public static void ThrowIfGreaterThan(uint value, uint other, string paramName, string message)
129+
{
130+
if (value <= other) { return; }
131+
132+
throw new ArgumentOutOfRangeException(paramName, message);
133+
}
134+
135+
public static void ThrowIfEqualToOrGreaterThan(uint value, uint other, string paramName, string message)
136+
{
137+
if (value < other) { return; }
138+
139+
throw new ArgumentOutOfRangeException(paramName, message);
140+
}
141+
142+
// ======== float ========
143+
144+
public static void ThrowIfZero(float value, string paramName, string message)
145+
{
146+
if (value != 0) { return; }
147+
148+
throw new ArgumentOutOfRangeException(paramName, message);
149+
}
150+
151+
public static void ThrowIfNegative(float value, string paramName, string message)
152+
{
153+
if (value >= 0) { return; }
154+
155+
throw new ArgumentOutOfRangeException(paramName, message);
156+
}
157+
158+
public static void ThrowIfZeroOrNegative(float value, string paramName, string message)
159+
{
160+
if (value > 0) { return; }
161+
162+
throw new ArgumentOutOfRangeException(paramName, message);
163+
}
164+
165+
public static void ThrowIfLessThan(float value, float other, string paramName, string message)
166+
{
167+
if (value >= other) { return; }
168+
169+
throw new ArgumentOutOfRangeException(paramName, message);
170+
}
171+
172+
public static void ThrowIfEqualToOrLessThan(float value, float other, string paramName, string message)
173+
{
174+
if (value > other) { return; }
175+
176+
throw new ArgumentOutOfRangeException(paramName, message);
177+
}
178+
179+
public static void ThrowIfGreaterThan(float value, float other, string paramName, string message)
180+
{
181+
if (value <= other) { return; }
182+
183+
throw new ArgumentOutOfRangeException(paramName, message);
184+
}
185+
186+
public static void ThrowIfEqualToOrGreaterThan(float value, float other, string paramName, string message)
187+
{
188+
if (value < other) { return; }
189+
190+
throw new ArgumentOutOfRangeException(paramName, message);
191+
}
192+
193+
// ======== double ========
194+
195+
public static void ThrowIfZero(double value, string paramName, string message)
196+
{
197+
if (value != 0) { return; }
198+
199+
throw new ArgumentOutOfRangeException(paramName, message);
200+
}
201+
202+
public static void ThrowIfNegative(double value, string paramName, string message)
203+
{
204+
if (value >= 0) { return; }
205+
206+
throw new ArgumentOutOfRangeException(paramName, message);
207+
}
208+
209+
public static void ThrowIfZeroOrNegative(double value, string paramName, string message)
210+
{
211+
if (value > 0) { return; }
212+
213+
throw new ArgumentOutOfRangeException(paramName, message);
214+
}
215+
216+
public static void ThrowIfLessThan(double value, double other, string paramName, string message)
217+
{
218+
if (value >= other) { return; }
219+
220+
throw new ArgumentOutOfRangeException(paramName, message);
221+
}
222+
223+
public static void ThrowIfEqualToOrLessThan(double value, double other, string paramName, string message)
224+
{
225+
if (value > other) { return; }
226+
227+
throw new ArgumentOutOfRangeException(paramName, message);
228+
}
229+
230+
public static void ThrowIfGreaterThan(double value, double other, string paramName, string message)
231+
{
232+
if (value <= other) { return; }
233+
234+
throw new ArgumentOutOfRangeException(paramName, message);
235+
}
236+
237+
public static void ThrowIfEqualToOrGreaterThan(double value, double other, string paramName, string message)
238+
{
239+
if (value < other) { return; }
240+
241+
throw new ArgumentOutOfRangeException(paramName, message);
242+
}
243+
}

service/Abstractions/Diagnostics/DefaultLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ private static bool TryGetLogLevel(string? value, out LogLevel level)
7171
return true;
7272
}
7373

74-
throw new ConfigurationException($"Log level not supported: {value}");
74+
throw new ConfigurationException($"Logger: log level '{value}' not supported");
7575
}
7676
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
#if NETSTANDARD2_0
4+
5+
// ReSharper disable CheckNamespace
6+
namespace System.Diagnostics.CodeAnalysis;
7+
8+
/// <summary>
9+
/// Specifies that an output is not null even if the corresponding type allows it.
10+
/// Specifies that an input argument was not null when the call returns.
11+
/// See https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.notnullattribute
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
14+
internal sealed class NotNullAttribute : Attribute
15+
{
16+
}
17+
#endif

0 commit comments

Comments
 (0)